如何在mongodb中访问第二级数组(与组)

时间:2019-06-16 12:02:53

标签: 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
                             }
                             ]
               }
         }
      ]
   }
]

我想要这样的结果 父母的年龄和孩子的身高,以及grandSons计数,grandGrandSons计数和儿童的喜欢计数

{
    "_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)

}},

我使用了查找功能,以获取上面的收藏(子)

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

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

我该怎么办?

1 个答案:

答案 0 :(得分:0)

$reduce添加到小组赛阶段:

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