首页 > 解决方案 > 如何访问mongodb中的二级数组(带组)

问题描述

这是样本集合:

{  
   "_id":ObjectId("5cc7d8e88c33e065c56b0883"),
   "age":70,
   "child":[  
         {  
            "id":"son1",
            "age":40,
            "grandSon":[  
               {  
                  "id":"grand1",
                  "age":10,
                  "like":[
                         {
                              "id":"like1",
                              "info":"apple"
                          },

                          {
                             "id":"like2",
                              "info":"banana"
                          }
                          ],
                  "grandGrandSon" :[
                            {
                                "id":"grandGrand1",
                                "age":10
                            },

                            {
                               "id":"grandGrand2",
                                "age":13
                            }
                            ]
               },
               {  
                  "id":"grand2",
                  "age":13,
                  "like":[
                         {
                              "id":"like1",
                              "info":"apple"
                          }
                          ],
                  "grandGrandSon" :[
                            {
                               "id":"grandGrand1",
                                "age":12
                            },

                            {
                               "id":"grandGrand2",
                                "age":14
                            }
                            ]
               }
            ]
         },
         {  
            "id":"son2",
            "age":40,
            "grandSon":[  
               {  
                  "id":"grand1",
                  "age":10,
                  "like":[
                         {
                              "id":"like1",
                              "info":"apple"
                          },
                          {
                              "id":"like2",
                              "info":"banana"
                          }
                          ],
                  "grandGrandSon" :[
                            {
                               "id":"grandGrand1",
                               "age":15
                            },

                            {
                               "id":"grandGrand2",
                               "age":16
                            }
                            ]
               },
               {  
                  "id":"grand2",
                  "age":14,
                  "like":[
                   {
                         "id":"like1",
                         "info":"apple"
                     },
                     {
                         "id":"like2",
                         "info":"banana"
                      }
                     ],
                  "grandGrandSon" :[
                             {
                                "id":"grandGrand1",
                                 "age":12
                             },

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

我想得到这样的结果

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

这是我的代码

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

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

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

}},

我使用查找,为上面的集合(子)

它返回孙子计数,但我想获得孙子计数

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

我该怎么办??

标签: mongodb

解决方案


$reduce添加到小组赛阶段:

    { 
        "$group" : {
            ....
            ....
            "grandGrandSonsCount" : {
                "$sum" : {
                    "$reduce" : {
                        "input" : "$child.grandSon", 
                        "initialValue" : 0, 
                        "in" : {
                            "$sum" : [
                                "$$value", 
                                {
                                    "$size" : "$$this.grandGrandSon"
                                }
                            ]
                        }
                    }
                }
            }
        }
    }

推荐阅读