MongoDB-使用addToSet计数项目

时间:2018-07-19 00:09:50

标签: mongodb mongodb-query aggregation-framework

我正在使用一个名为“提交”的集合,其中包含游戏的任务数据。每次您提交问题/任务的答案时,都会将文件保存到提交集合中。

典型文档如下:

{ 
    "_id" : ObjectId("5b0c99dffea598002fdb6ec9"), 
    "status" : "Accepted", 
    "missionAcceptanceDate" : NumberInt(1527526261), 
    "submissionDate" : NumberInt(1527552495), 
    "location" : "", 
    "approvalDate" : null, 
    "mission_id" : ObjectId("5b0c5b10fea598002fdb6ec6"), 
    "user_id" : ObjectId("5b0c99d0fea598002fdb6ec7")
}

我需要构建一个可以确定以下内容的查询:

  1. 在日期范围内按状态对每个分组的提交总数进行计数,
  2. 按每个分组的Mission_id计算提交的总数。

以下是我到目前为止所拥有的(“删除一些信息...”):

查询

db.submissions.aggregate(
        { $match: {
            "submissionDate": {
                "$gte": NumberInt(1527901260), //Fri, 1 Jun 2018 18:00:01 PDT
                "$lte": NumberInt(1530831600) //Thu, 5 Jul 2018 16:00:00 PDT
            }
          }
        },
        {$project: 
            {_id:1,
            status: 1,
            missionAcceptanceDate: 1,
            submissionDate: 1,
            approvalDate: 1,
            user_id: 1,
            mission_id: 1
            }
        },
        {$group: {
                    _id: { status: "$status" },
                    statusTotal: { $sum: 1 },
                    mission_id: { $addToSet: "$mission_id" },
                    }
         }

)

结果

{ 
    "_id" : {
        "status" : "Rejected"
    }, 
    "statusTotal" : 16.0, 
    "mission_id" : [
        ObjectId("5b0edbfafea598002fdb6f3a"), 
        ObjectId("5b0f0131fea598002fdb6f43"), 
        ObjectId("5b0eded6fea598002fdb6f3d"), 
        ...
    ]
}
{ 
    "_id" : {
        "status" : "Approved"
    }, 
    "statusTotal" : 592.0, 
    "mission_id" : [
        ObjectId("5b391cf4e177c700308dd0ef"), 
        ObjectId("5b36a0d172b5240030d304be"), 
        ObjectId("5b3558276a0f950030db1732"), 
        ...
    ]
}

这很棒,但是我还需要知道每个“ mission_id”被“拒绝”或“批准”的总次数。所需的输出应该类似于以下内容:

{ 
    "_id" : {
        "status" : "Rejected"
    }, 
    "statusTotal" : 16.0, 
    "mission_id" : [

        ...
        { mission_id: ObjectId("5b0edbfafea598002fdb6f3a"), count: 3 },
        { mission_id: ObjectId("5b0f0131fea598002fdb6f43"), count: 5},
        { mission_id: ObjectId("5b0eded6fea598002fdb6f3d"), count: 2 } 
        ...
    ]
}
{ 
    "_id" : {
        "status" : "Approved"
    }, 
    "statusTotal" : 592.0, 
    "missionData" : [ 
        { mission_id: ObjectId("5b391cf4e177c700308dd0ef"), count: 23 },
        { mission_id: ObjectId("5b36a0d172b5240030d304be"), count: 45},
        { mission_id: ObjectId("5b3558276a0f950030db1732"), count: 15 } 
        ...
    ]
}

我尝试使用$ size,$ sum,$ count,甚至为每个mission_id使用第二个$ {$ sum:1}的$ group,但无法弄清楚。任何帮助,将不胜感激。预先谢谢你!

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则需要在小组之前进行额外的分组。

类似

db.submissions.aggregate([
  {"$match":{"submissionDate":{"$gte":NumberInt(1527901260),"$lte":NumberInt(1530831600)}}},
  {"$group":{
    "_id":{"status":"$status","mission_id":"$mission_id"},
    "missionTotal":{"$sum":1}
  }},
  {"$group":{
    "_id":"$_id.status",
    "statusTotal":{"$sum":"$missionTotal"},
    "mission_id":{"$push":{"mission_id":"$_id.mission_id","count":"$missionTotal"}}
  }}
])