Mongo DB Aggregation管道用于嵌套文档构建的帮助

时间:2019-07-13 17:17:04

标签: mongodb mongodb-query aggregation-framework

尝试了mongodb聚合和管道概念中的多种方法,但无法完全实现项目所需的以下格式。

我在Mongodb集合中的数据采用以下格式

[{
    "title" : "Product Title 1",
    "desc" : "Product Description 1",
    "year" : 2019,
    "productkey" : "A"
},
{
    "title" : "Product Title 2",
    "desc" : "Product Description 2",
    "year" : 2019,
    "productkey" : "A"
},,
{
    "title" : "Product Title 3",
    "desc" : "Product Description 3",
    "year" : 2018,
    "productkey" : "A"
},
{
    "title" : "Product Title 4",
    "desc" : "Product Description 4",
    "year" : 2018,
    "productkey" : "A"
},
[{
    "title" : "Product Title 5",
    "desc" : "Product Description 5",
    "year" : 2019,
    "productkey" : "B"
},
{
    "title" : "Product Title 6",
    "desc" : "Product Description 6",
    "year" : 2019,
    "productkey" : "B"
},
{
    "title" : "Product Title 7",
    "desc" : "Product Description 7",
    "year" : 2018,
    "productkey" : "B"
},
{
    "title" : "Product Title 8",
    "desc" : "Product Description 8",
    "year" : 2018,
    "productkey" : "B"
},
{
    "title" : "Product Title 9",
    "desc" : "Product Description 9",
    "year" : 2019,
    "productkey" : "C"
},
{
    "title" : "Product Title 10",
    "desc" : "Product Description 10",
    "year" : 2019,
    "productkey" : "C"
},
{
    "title" : "Product Title 11",
    "desc" : "Product Description 11",
    "year" : 2018,
    "productkey" : "C"
}] 

我正在尝试使用聚合和管道实现以下格式

[{
    "productkey" : "A",
    "details":
    [
        {
            "year": 2019,
            "subdetails":[
                {
                    "title" : "Product Title 1",
                    "desc" : "Product Description 1",
                },
                {
                    "title" : "Product Title 2",
                    "desc" : "Product Description 2",
                }           
            ]
        },
        {
            "year": 2018,
            "subdetails":[
                {
                    "title" : "Product Title 3", "desc" : "Product Description 3",
                },
                {
                    "title" : "Product Title 4",
                    "desc" : "Product Description 4",
                }           
            ]

        } 
    ]
},
{
    "productkey" : "B",
    "details":
    [
        {
            "year": 2019,
            "subdetails":[
                {
                    "title" : "Product Title 5",
                    "desc" : "Product Description 5",
                },
                {
                    "title" : "Product Title 6",
                    "desc" : "Product Description 6",
                }           
            ]
        },
        {
            "year": 2018,
            "subdetails":[
                {
                    "title" : "Product Title 7",
                    "desc" : "Product Description 7",
                },
                {
                    "title" : "Product Title 8",
                    "desc" : "Product Description 8",
                }           
            ]

        } 
    ]
},
{
    "productkey" : "C",
    "details":
    [
        {
            "year": 2019,
            "subdetails":[
                {
                    "title" : "Product Title 9",
                    "desc" : "Product Description 9",
                },
                {
                    "title" : "Product Title 10",
                    "desc" : "Product Description 10",
                }           
            ]
        },
        {
            "year": 2018,
            "subdetails":[
                {
                    "title" : "Product Title 11",
                    "desc" : "Product Description 11",
                }       
            ]

        } 
    ]
}]

那我该如何实现呢?

1 个答案:

答案 0 :(得分:1)

您可以使用以下汇总

db.collection.aggregate([
  { "$group": {
    "_id": { "productkey": "$productkey", "year": "$year" },
    "subDetails": { "$push": { "title": "$title", "desc": "$desc" }}
  }},
  { "$group": {
    "_id": "$_id.productkey",
    "details": { "$push": { "year": "$_id.year", "subDetails": "$subDetails" }}
  }}
])

MongoPlayground