Aggregate嵌套子文档

Aggregate嵌套子文档

本文介绍了Mongodb Aggregate嵌套子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有文档列表

[
{
    "__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在每个文档中都是唯一的,因此从理论上讲,这应该更简单.

I would like to aggregate the list of users aka. elements.votes.meta.createdBy across the documents and count the total numbers of occurrences across the document. *Note that the elements.votes.meta.createdBy is unique per document, so in theory this should make is simpler.

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

So far, I've ended up with a query:

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

只是要再次完全卡住.我知道我需要一个双重分组,只是似乎无法弄清楚.任何帮助表示赞赏.

Just to get completely stuck again. I know I need a double grouping, just can't seem to be able to figure it out. Any help appreciated.

推荐答案

首先,您应该获取每个用户"的总数. (即 {$ group:{_id:'$ user',count:{'$ sum':1}}} )

First you should get the totals for each 'user'. (i.e. { $group: {_id: '$user', count: {'$sum': 1} }})

然后按空分组以创建一个带有结果的文档,添加每个要设置的用户并将结果从第一次分组推入数组字段. (第二分组)

Then just group by null to create a document with the result, add each user to set and push the result from first grouping onto array field. (2nd grouping)

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
}

这篇关于Mongodb Aggregate嵌套子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:59