【问题标题】:fetch issue in multiple nested array document from MongoDB从 MongoDB 获取多个嵌套数组文档中的问题
【发布时间】:2018-02-24 18:53:38
【问题描述】:

我在每个级别的每个文档中都有多个嵌套数组。我必须获取符合以下条件的文档:

1. empId : 123
2. address.country: "AUS"
3. group.primaryGroup.primary:"Y"
4. group.subGroup.primarySubGroup.primary : "Y"
5. group.subGroup.primarySubGroup.country : "AUS"
6. In group[0], if primaryGroup.primary = "Y" is not matching then ignore the group[0].
7. In group[0], if subGroup.primarySubGroup.primary = "Y" and subGroup.primarySubGroup.country = "AUS" is not matching then also ignore the entire group[0].
8. If primaryGroup and primarySubGroup matching the criteria then I have to fetch the particular group , primaryGroup and primarySubGroup array.
9. If none of the group[].primaryGroup[] is matching the criteria then I can ignore the entire document.
10. If none of the group[].subGroup[].subPrimaryGroup[] is matching the criteria then I can ignore the entire document.

我无法应用上面提到的标准“第 7 点”。

下面给出我尝试过的查询:

db.temp.aggregate([{"$match": {"empId": 123,"address.country": "AUS","group.primaryGroup.primary": "Y","group.subGroup.primarySubGroup.primary": "Y","group.subGroup.primarySubGroup.country": "AUS"}}, {"$project": {"empId": 1,"mobile": 1,"address": {"$filter": {"input": "$address","as": "d","cond": {"$eq": ["$$d.country", "AUS"]}}},"group": {"$map": {"input": {"$filter": {"input": "$group","as": "b","cond": {"$in": ["Y", "$$b.primaryGroup.primary"]}}}, "as": "vp","in": {"groupId": "$$vp.groupId","primaryGroup": {"$filter": {"input": "$$vp.primaryGroup","as": "vp","cond": {"$eq": ["$$vp.primary", "Y"]}}},"subGroup": {"$map": {"input": {"$filter": {"input": "$$vp.subGroup","as": "np","cond": {"$and": [{"$in": ["Y", "$$np.primarySubGroup.primary"]}, {"$in": ["AUS", "$$np.primarySubGroup.country"]}]}}},"as": "n","in": {"subGroupId": "$$n.subGroupId","primarySubGroup": {"$filter": {"input": "$$n.primarySubGroup","as": "n","cond": {"$and": [{"$eq": ["$$n.primary", "Y"]}, {"$eq": ["$$n.country", "AUS"]}]}}}}}}}}}}}]).pretty()

我参考了网址 (MongoDB nested array search using $map)

输入:

[
    {
        "empId": 123,
        "address": [
            {
                "street": "no.12 wilson street",
                "country":"AUS"
            },
            {
                "description": "No.32 watson street",
                "country":"CAN"
            }
        ],
        "mobile": 2387468238,
        "group": [
            {
                "groupId": 75227,
                "primaryGroup": [
                    {
                        "primary": "Y"
                    },
                    {
                        "primary": "N"
                    }
                ],
                "subGroup": [
                    {
                        "subGroupId": 123,
                        "primarySubGroup": [
                            {
                                "primary": "Y",
                                "country": "AUS"
                            },
                            {
                                "primary": "N",
                                "country": "IND"
                            }
                        ]
                    },
                    {
                        "subGroupId": 234,
                        "primarySubGroup": [
                            {
                                "primary": "N",
                                "country": "USA"
                            },
                            {
                                "primary": "Y",
                                "country": "IND"
                            }
                        ]
                    },
                    {
                        "subGroupId": 432,
                        "primarySubGroup": [
                            {
                                "primary": "Y",
                                "country": "AUS"
                            },
                            {
                                "primary": "N",
                                "country": "CAN"
                            }
                        ]
                    }
                ]
            }, {
                "groupId": 33333,
                "primaryGroup": [
                    {
                        "primary": "Y"
                    },
                    {
                        "primary": "N"
                    }
                ],
                "subGroup": [
                    {
                        "subGroupId": 6734,
                        "primarySubGroup": [
                            {
                                "primary": "Y",
                                "country": "CAN"
                            },
                            {
                                "primary": "N",
                                "country": "IND"
                            }
                        ]
                    },
                    {
                        "subGroupId": 9864,
                        "primarySubGroup": [
                            {
                                "primary": "N",
                                "country": "IND"
                            },
                            {
                                "primary": "Y",
                                "country": "USA"
                            }
                        ]
                    }
                ]
            }, {
                "groupId": 44444,
                "primaryGroup": [
                    {
                        "primary": "N"
                    },
                    {
                        "primary": "N"
                    }
                ],
                "subGroup": [
                    {
                        "subGroupId": 6734,
                        "primarySubGroup": [
                            {
                                "primary": "Y",
                                "country": "AUS"
                            },
                            {
                                "primary": "N",
                                "country": "IND"
                            }
                        ]
                    },
                    {
                        "subGroupId": 9864,
                        "primarySubGroup": [
                            {
                                "primary": "N",
                                "country": "IND"
                            },
                            {
                                "primary": "Y",
                                "country": "USA"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

输出:

[
    {
        "empId": 123,
        "address": [
            {
                "street": "no.12 wilson street",
                "country":"AUS"
            }
        ],
        "mobile": 2387468238,
        "group": [
            {
                "groupId": 75227,
                "primaryGroup": [
                    {
                        "primary": "Y"
                    }
                ],
                "subGroup": [
                    {
                        "subGroupId": 123,
                        "primarySubGroup": [
                            {
                                "primary": "Y",
                                "country": "AUS"
                            }
                        ]
                    },
                    {
                        "subGroupId": 432,
                        "primarySubGroup": [
                            {
                                "primary": "Y",
                                "country": "AUS"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

你能帮我解决这个问题吗?提前致谢。

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework spring-data-mongodb


    【解决方案1】:

    您的聚合管道中存在拼写错误和过滤器问题

    1. 子组过滤器中缺少$ 前缀
    2. 添加了 $filter 以删除具有 0 个子组的组

    管道

    db.temp.aggregate([
       {
          "$match":{
             "empId":123,
             "address.country":"AUS",
             "group.primaryGroup.primary":"Y",
             "group.subGroup.primarySubGroup.primary":"Y",
             "group.subGroup.primarySubGroup.country":"AUS"
          }
       },
       {
          "$project":{
             "empId":1,
             "mobile":1,
             "address":{
                "$filter":{
                   "input":"$address",
                   "as":"d",
                   "cond":{
                      "$eq":[ "$$d.country","AUS" ]
                   }
                }
             },
             "group":{
                "$map":{
                   "input":{
                      "$filter":{
                         "input":"$group",
                         "as":"b",
                         "cond":{
                            $and : [
                                { "$in":[ "Y", "$$b.primaryGroup.primary" ] }
                            ]
                         }
                      }
                   },
                   "as":"vp",
                   "in":{
                      "groupId":"$$vp.groupId",
                      "primaryGroup":{
                         "$filter":{
                            "input":"$$vp.primaryGroup",
                            "as":"vc",
                            "cond":{
                               "$eq":[ "$$vc.primary", "Y" ]
                            }
                         }
                      },
                      "subGroup":{
                         "$map":{
                            "input":{
                               "$filter":{
                                  "input":"$$vp.subGroup",
                                  "as":"np",
                                  "cond":{
                                     "$and":[
                                        { "$in":[ "Y", "$$np.primarySubGroup.primary" ] },
                                        { "$in":[ "AUS","$$np.primarySubGroup.country" ] }
                                     ]
                                  }
                               }
                            },
                            "as":"n",
                            "in":{
                               "subGroupId":"$$n.subGroupId",
                               "primarySubGroup":{
                                  "$filter":{
                                     "input":"$$n.primarySubGroup",
                                     "as":"mp",
                                     "cond":{
                                        "$and":[
                                           { "$eq":[ "$$mp.primary", "Y" ] },
                                           { "$eq":[ "$$mp.country", "AUS" ] }
                                        ]
                                     }
                                  }
                               }
                            }
                         }
                      }
                   }
                }
             }
          }
       },
       {"$project":
            {
                 "empId":1,
                 "mobile":1,
                 "address":1,
                 "group":{
                    $filter : {
                        input : "$group",
                        as : "g",
                        cond : {$gt : [{$size : "$$g.subGroup"}, 0]}
                    }
                }
            }
        }
    ]).pretty()
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多