Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 用于嵌套文档生成的Mongo DB聚合管道帮助_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb 用于嵌套文档生成的Mongo DB聚合管道帮助

Mongodb 用于嵌套文档生成的Mongo DB聚合管道帮助,mongodb,mongodb-query,aggregation-framework,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

在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",
                }       
            ]

        } 
    ]
}]

那么我如何才能做到这一点呢?

您可以使用下面的聚合

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" }}
  }}
])

在上述解决方案中,是否可以在详细信息级别按年份进行排序?在
$group
{$sort:{“details.year”:1}}两个组之间添加一个
$sort
阶段,尝试在组之间添加,但运气不佳
{$sort:{“\id.year”:1}