繁体   English   中英

MongoDB多重聚合匹配和分组

[英]Mongodb multiple aggregation match and group

我正在获取类别和子类别,我正在使用以下管道Category:

{
                "$match": {
                    "category": {
                         "$in": ["list of my categories"]
                    }
                }
            },
            {
                "$group": {
                    "category": "$category",
                    "count": {
                        "$sum": 1
                    }
                }
            },
            

这给了我:

{category: category name,
count: totalcount}

Subcategory pipeline

            {
                "$match": {
                    "category": {
                        "$in": ["list of my categories"]
                    }
                }
            },
            {
                "$group": {
                    "_id": { subCategory: "$subCategory", category: "$category" },
                    "count": {
                        "$sum": 1
                    }
                }
            },
            {
                "$group": {
                    "_id": "$_id.category",
                    "counts": {
                        "$push": {
                            "k": "$_id.subCategory",
                            "v": "$count"
                        },
                    },
                    "count":{
                        $sum: "$counts"
                    }
                }
            },
            {
                "$project": {
                    "counts": { "$arrayToObject": "$counts" },
                }
            },

这给了我

category: name {

subcategory1 : total count,
...
}

我如何加入两者以获得一个电话以返回类似的东西

{category: categoryname,
count: totalcountforcategory,
subcategories: {
subcategory: totalcount,
subcategory2:totalcount}

更新

这是我的示例 JSON

{
    "category": "Category one",
    "name": "Sample name",
    "subCategory": "subCategory one",
},
{
    "category": "Category one",
    "name": "Sample name",
    "subCategory": "subCategory two",
},
{
    "category": "Category two",
    "name": "Sample name",
    "subCategory": "subCategory one",
},
{
    "category": "Category one",
    "name": "Sample name",
    "subCategory": "subCategory two",
}

预期产出

{
"Category one": 3,
subCategories: {
"subCategoryone": 2,
"subCategorytwo":3,
}
}

{
"Category two": 5,
subCategories: {
"subCategoryone": 2,
"subCategorytwo":3,
}
}
db.collection.aggregate([
  {
    "$group": {
      "_id": {
        cat: "$category",
        sub: "$subCategory"
      },
      "count": {
        "$sum": 1
      },
      "subCategory": {
        $push: "$$ROOT"
      }
    }
  },
  {
    "$group": {
      "_id": "$_id.cat",
      "counts": {
        "$push": {
          "k": "$_id.sub",
          "v": "$count"
        },
        
      },
      "count": {
        $sum: "$counts"
      }
    }
  },
  {
    "$project": {
      "counts": {
        "$arrayToObject": "$counts"
      },
      
    }
  },
])

操场

我使用$facet加入管道我不知道这是否是最佳实践

const pipeline = [
        {
            "$facet": {
                "GroupAll":
                    [{ $project: { name: 1, category: 1,subCategory:1,votes:1 } }]
                ,
                "GroupTotal": [
                    {
                        "$match": {
                            "nominationYear": {
                                "$eq": "2022"
                            }
                        }
                    },
                    {
                        $count: "total"
                    }
                ],
                "GroupCategories": [
                    {
                        "$match": {
                            "category": {
                                "$in": ["Categories"]
                            }
                        }
                    },
                    {
                        "$group": {
                            "_id": { category: "$category" },
                            "count": {
                                "$sum": 1
                            }
                        }
                    },
                ],
                "GroupSubCategories": [
                    {
                        "$match": {
                            "category": {
                                "$in": ["Categories"]
                            }
                        }
                    },
                    {
                        "$group": {
                            "_id": { subCategory: "$subCategory", category: "$category" },
                            "count": {
                                "$sum": 1
                            }
                        }
                    },

                    {
                        "$group": {
                            "_id": "$_id.category",
                            "counts": {
                                "$push": {
                                    "k": "$_id.subCategory",
                                    "v": "$count"
                                },
                            },
                            "count": {
                                $sum: "$counts"
                            }
                        }
                    },

                    {
                        "$project": {
                            "counts": { "$arrayToObject": "$counts" },
                        }
                    },
                ]
            }
        }

    ]

这是我的输出

    {
  "nominations": [
    {
      "GroupAll": [""],
      "GroupTotal": [""],
      "GroupCategories": [""],
      "GroupSubCategories":[""]
    }
  ],
  "total": ""
}

我使用$facet来加入多个管道来实现它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM