Mongodb Aggregate嵌套子文档

时间:2015-02-07 21:29:29

标签: mongodb mongodb-query

有文件清单

[
{
    "__v" : 21,
    "_id" : ObjectId("546330dbb8926d177052e9ff"),
    "code" : "WfvCc",
    "description" : "",
    "elements" : [ 
        {
            "_id" : ObjectId("546471f61e13b76a0b20ccaf"),
            "comments" : [],
            "meta" : {
                "createdBy" : "545ab39ef1b0c88a695fcf8d",
                "modifiedAt" : "1415868918045",
                "createdAt" : "1415868918045"
            },
            "title" : "awesome title",
            "votes" : {
                "count" : 3,
                "meta" : [ 
                    {
                        "createdBy" : "545ab39ef1b0c88a695fcf8d",
                        "_id" : ObjectId("546473831e13b76a0b20ccb7"),
                        "createdAt" : "1415869315618"
                    }, 
                    {
                        "createdBy" : "545aaddcf1b0c88a695fcf84",
                        "_id" : ObjectId("546473d71e13b76a0b20ccbc"),
                        "createdAt" : "1415869399584"
                    }, 
                    {
                        "createdBy" : "5461c0e2c9c39a192c44226c",
                        "_id" : ObjectId("546474041e13b76a0b20ccbe"),
                        "createdAt" : "1415869444056"
                    }
                ]
            }
        }
    ]
},
{
    "__v" : 21,
    "_id" : ObjectId("546330dbb8926d177052e9ff"),
    "code" : "WfvCc",
    "description" : "",
    "elements" : [ 
        {
            "_id" : ObjectId("546471f61e13b76a0b20ccaf"),
            "comments" : [],
            "meta" : {
                "createdBy" : "545ab39ef1b0c88a695fcf8d",
                "modifiedAt" : "1415868918045",
                "createdAt" : "1415868918045"
            },
            "title" : "awesome title",
            "votes" : {
                "count" : 3,
                "meta" : [ 
                    {
                        "createdBy" : "545ab39ef1b0c88a695fcf8d",
                        "_id" : ObjectId("546473831e13b76a0b20ccb7"),
                        "createdAt" : "1415869315618"
                    }, 
                    {
                        "createdBy" : "545aaddcf1b0c88a695fcf84",
                        "_id" : ObjectId("546473d71e13b76a0b20ccbc"),
                        "createdAt" : "1415869399584"
                    }, 
                    {
                        "createdBy" : "5461c0e2c9c39a192c44226c",
                        "_id" : ObjectId("546474041e13b76a0b20ccbe"),
                        "createdAt" : "1415869444056"
                    }
                ]
            }
        }
    ]
}
]

我想汇总用户列表。跨文档elements.votes.meta.createdBy并计算文档中出现的总次数。 *请注意,elements.votes.meta.createdBy对于每个文档都是唯一的,因此理论上这应该更简单。

到目前为止,我最终得到了一个查询:

db.sessions.aggregate(
  { $project: { 
      meta: "$elements.votes.meta"
  }},
  { $unwind: "$meta" },
  { $group: {
      _id: "voters",
      voters: {
          $addToSet: "$meta.createdBy"
      }
  }}
)

再次被完全卡住了。我知道我需要一个双重分组,似乎无法弄明白。任何帮助赞赏。

1 个答案:

答案 0 :(得分:9)

首先,您应该获得每个用户的总数。 (即 {$ group:{_ id:' $ user',count:{' $ sum':1}}}

然后只需按null分组以创建包含结果的文档,添加每个用户进行设置并将结果从第一个分组推送到数组字段。 (第2组)

db.test5.aggregate(
  { $unwind: "$elements" },
  { $unwind: "$elements.votes.meta" },
  { $project: {_id: '$_id', user: '$elements.votes.meta.createdBy'} },
  { $group: {_id: '$user', count: {'$sum': 1} }},
  { $group: {
        _id: null, 
        users: {$addToSet: '$_id'}, 
        occurances: {$push: {'user': '$_id', count: '$count'}}
        }
   }
)

结果:

{
    "result" : [ 
        {
            "_id" : null,
            "users" : [ 
                "545ab39ef1b0c88a695fcf8d", 
                "545aaddcf1b0c88a695fcf84", 
                "5461c0e2c9c39a192c44226c"
            ],
            "occurances" : [ 
                {
                    "user" : "5461c0e2c9c39a192c44226c",
                    "count" : 2
                }, 
                {
                    "user" : "545aaddcf1b0c88a695fcf84",
                    "count" : 2
                }, 
                {
                    "user" : "545ab39ef1b0c88a695fcf8d",
                    "count" : 2
                }
            ]
        }
    ],
    "ok" : 1
}