小编Squ*_*ker的帖子

mongodb 的嵌套聚合管道

我有一个 mongodb 集合,其中包含以下格式的大量文档:

{
    "_id": {
        "$oid": "581f9eae05711c35f461d779"
    },
    "eventID": "Illinois84bc00edc91c24a95573",
    "type": "voteup",
    "update": "f3a44be56669e01cf02ed685c97451ba",
    "count": 1,
}
Run Code Online (Sandbox Code Playgroud)

我需要能够从我的服务器对集合运行聚合并返回一个 JSON 对象,其中:

按 eventID(有许多 eventID)对数据进行分组,然后按更新对这些组中的文档进行分组(有许多更新,每个更新都与一个 eventID 相关联),然后对于每个 eventID/Update 组,将每个 eventID/Update 组的“计数”值相加类型为“voteup”和“votedown”的文档。

所以实际的输出可能如下所示:

{
    "events": [
    //  array of objects, one for each unique eventID
        {
            "eventID": "Idaho4532543trt3424f",
            "updates": [
            //  array of objects, one for each unique update
                {
                    "update": "rh43782ty738tywuioty34",
                    "voteup": 12,  // the number of documents with that eventID/update which are type "voteup"
                    "votedown": 1  // the number of …
Run Code Online (Sandbox Code Playgroud)

mongodb aggregation-framework

4
推荐指数
1
解决办法
8326
查看次数

标签 统计

aggregation-framework ×1

mongodb ×1