在mongo中使用group时,如何获得嵌套数组大小的嵌套数组?

时间:2019-06-14 05:45:42

标签: mongodb

这是收藏

{  
   "_id":ObjectId("5cc7d8e88c33e065c56b0883"),
   "age":70,
   "child":[  
         {  
            "id":"son1",
            "age":40,
            "grandSon":[  
               {  
                  "id":"grand1",
                  "age":10
                  "grandGrandSon" :[
                            {
                                "id":"grandGrand1",
                                "age":10
                            },

                            {
                               "id":"grandGrand2",
                                "age":13
                            }
                            ]
               },
               {  
                  "id":"grand2",
                  "age":13
                  "grandGrandSon" :[
                            {
                               "id":"grandGrand1",
                                "age":12
                            },

                            {
                               "id":"grandGrand2",
                                "age":14
                            }
                            ]
               }
            ]
         },
         {  
            "id":"son2",
            "age":40,
            "grandSon":[  
               {  
                  "id":"grand1",
                  "age":10
                  "grandGrandSon" :[
                            {
                               "id":"grandGrand1",
                               "age":15
                            },

                            {
                               "id":"grandGrand2",
                               "age":16
                            }
                            ]
               },
               {  
                  "id":"grand2",
                  "age":14
                  "grandGrandSon" :[
                             {
                                "id":"grandGrand1",
                                 "age":12
                             },

                             {
                                "id":"grandGrand2",
                                 "age":13
                             }
                             ]
               }
         }
      ]
   }
]

我想要这样的结果 父母的年龄和孩子的大小以及grandSons计数,grandGrandSons计数

{
    "_id": ObjectId("5cc7d8e88c33e065c56b0883"),
    "age": 70,
    "childCount": 2, 
    "grandSonsCount": 4
    "grandGrandSonsCount": 8, //important
    "grandSonsAgeCount": 105 // not important
}

这是我的代码

{
    $lookup:
    {
        from: "..."
        localField: "...",
        foreignField: "..",
        as: "child"
    },

},
{ $unwind: "$child" }
{ $group : {
    _id : "$_id",
    age: {$first:"$age"},

    childCount: {$sum: 1},
    grandSonsCount : {$sum : {$size : "$child.grandSon"}},

    grandGrandSonsCount : 
    {$sum : {$sum : {$size : "$child.grandSon.grandGrandSon"}}}, 
    //it is return 4(grandSonsCount)

}},

我使用了查找功能,进行了上述收集(孩子) 我要修改

//{$sum : {$sum : {$size : "$child.grandSon.grandGrandSon"}}}

它返回grandSon计数,但我想获得grandGrandSon计数

如何获取嵌套数组中的嵌套数组?

我想获得grandGrandSon的大小

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要unwind的{​​{1}}数组和child数组才能获得grandSon数组的大小。

尝试一下:

grandGrandsons

只需添加childCount,这样我们在展开两个数组后就不会遇到计算childCount的任何问题。

详细了解$unwind here$addFields here