代码之家  ›  专栏  ›  技术社区  ›  Mansa

聚合组中MongoDB count fi true

  •  0
  • Mansa  · 技术社区  · 6 年前

    我试着用一种简单的方法来平均和总结一些统计数据,但却碰上了一堵墙!

    我收集了一些文件,其中包含如下统计数据:

    {
      "playerId": "5c6024b5031f5bc44d790e01",
      "courseId": "5b2a0ab1c6dc0f04e9a1e769",
      "gir": true,
      "data": {
        "roundHoles": [
          {
            "type": "HoleData",
            "holeNo": 1,
            "par": 3,
            "hcp": 18
          },
          {
            "type": "HoleData",
            "holeNo": 2,
            "par": 4,
            "hcp": 4
          },
          {
            "type": "HoleData",
            "holeNo": 3,
            "par": 4,
            "hcp": 8
          }
        ],
        "holeScores": [
          {
            "type": "RoundHoleData",
            "strokes": 3,
            "points": 2,
            "puts": 1,
            "gir": true,
            "scrambled": false
          },
          {
            "type": "RoundHoleData",
            "strokes": 5,
            "points": 1,
            "puts": 2,
            "gir": false,
            "scrambled": false
          },
          {
            "type": "RoundHoleData",
            "strokes": 4,
            "points": 2,
            "puts": 1,
            "gir": false,
            "scrambled": true
          }
        }
      }
    }
    

    我想做的是得到平均笔划,点数和推杆,加扰和gir之和,如果为真,但只有当主“gir”设置为真。

    以下是我到目前为止得出的结论:

    var allScores = runtimeCollection('roundScores').aggregate(
      {
          $match: { "playerId": playerId, "courseId": "5b2a0ab1c6dc0f04e9a1e769" }
      },
      {
          $unwind: {
              path: "$data.holeScores",
              includeArrayIndex: "index"
          }
      },
      {
          $group: {
              _id: "$index",
              rounds: { $sum: 1 },
              avgStrokes: { $avg: "$data.holeScores.strokes" },
              avgPoints: { $avg: "$data.holeScores.points" },
              avgPuts: { $avg: "$data.holeScores.puts" },
              sumScrambles: { $sum: "$data.holeScores.scrambled" }
          }
      }
    );
    

    以下是我从中得到的:

    {
      "_id": 17,
      "rounds": 18,
      "avgStrokes": 3.4444444444444446,
      "avgPoints": 1.2777777777777777,
      "avgPuts": 1.6111111111111112,
      "sumScrambles": 0
    },
    {
      "_id": 14,
      "rounds": 18,
      "avgStrokes": 5.388888888888889,
      "avgPoints": 2.1666666666666665,
      "avgPuts": 1.5,
      "sumScrambles": 0
    },
    {
      "_id": 12,
      "rounds": 18,
      "avgStrokes": 5,
      "avgPoints": 1.6111111111111112,
      "avgPuts": 1.8333333333333333,
      "sumScrambles": 0
    }
    

    看起来我得到的平均部分很好,但总和不起作用。我想我得给这些加个条件,但不知道怎么加?

    真的希望有人能帮我,提前谢谢:-)

    1 回复  |  直到 6 年前
        1
  •  1
  •   Elvis    6 年前

    首先,你不能得到布尔值的和,你只能得到数字的和。所以,我假设只要置乱值为真,就要加1,在这种情况下,可以使用' $cond ,它允许您编写条件。

    你可以试试这个,

    sumScrambles: { $sum: { $cond: [ { $eq: [ "$data.holeScores.scrambled", true ] }, 1, 0 ] } }
    
        2
  •  2
  •   chridam Gino Claudi    6 年前

    你不一定需要 $unwind $group 为了得到这个总数的平均值和总和,所有这些都可以在一个简单的时间内完成 $project 阶段 自从 $sum $avg $项目 假设字段是数组。

    至于条件和,你可以用 $filter $size 给你一笔钱。

    下面的管道演示了这种方法

    runtimeCollection('roundScores').aggregate([
        { '$match': { 'playerId': playerId, 'courseId': '5b2a0ab1c6dc0f04e9a1e769' } },
        { '$project': {
            'rounds': { '$size': '$data.holeScores' },
            'avgStrokes': { '$avg': '$data.holeScores.strokes' },
            'avgPoints': { '$avg': '$data.holeScores.points' },
            'avgPuts': { '$avg': '$data.holeScores.puts' },
            'avgPars': { '$avg': '$data.roundHoles.par' },
            'sumScrambles': { 
                '$size': {
                    '$filter': {
                        'input': '$data.holeScores',
                        'cond': '$$this.scrambled'
                    }
                }
            }
        } }
    ])
    
    推荐文章