MongoDB nested array search using $map

2019-08-20 01:31发布

I have collection which contains nested array. I need to fetch the data based on below condition:

empId : 19107
address.country: "AUS"
group.primaryGroup.primary:"Y"
group.subGroup.primarySubGroup.primary : "Y"

Input:

{
    "empId": "19107",
    "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"
                        },
                        {
                            "primary": "N"
                        }
                    ]
                },
                {
                    "subGroupId": 234,
                    "primarySubGroup": [
                        {
                            "primary": "N"
                        },
                        {
                            "primary": "Y"
                        }
                    ]
                }
            ]
        }
    ]
}

I need the output as below:

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

Below given the query which I tried:

[{"$match" : {"empId":90, "address" : {"$elemMatch": {"country": {"$eq":"AUS"}}}, "group" :{"$elemMatch" : {"primaryGroup": {"$elemMatch" : {"primary": {"$eq": "Y"}}}, "subGroup" : {"$elemMatch" : { "primarySubGroup" : { "$elemMatch": {"primary" : {"$eq" : "Y"}}}}}}}}}, {"$project": {"empId":1, "mobile":1, "address": {"$filter" : {"input": "$address", "as": "d", "cond": {"$eq": ["$$d.country", "AUS"]}}}  , "group" : {"$map": {"input": "$group", "as" : "v", "in": {"primaryGroup": {"$filter": {"input": "$$v.primaryGroup", "as": "vp", "cond": {"$eq": ["$$vp.primary", "Y"]}}}}}}, "subGroup": {"$map" : {"input": "$group", "as" : "n", "in": {"primarySubGroup" : {"$filter": {"input": "$$n.group", "as" : "mp", "cond": {"$eq": ["$$mp.primarySubGroup.primary", "830090"]}}}}}}  }}]

I am new to mongoDB. I tried the below approach (Spring data Match and Filter Nested Array) but I am facing some issue in nested array fetch. ex: Instead of groupId, I need to compare the primaryGroup in $map which is present in group field.

Could you please help me with this. Thanks in advance.

1条回答
Emotional °昔
2楼-- · 2019-08-20 01:57

You can use below query.

Couple of things I've changed.

1.No $elemMatch is required for single criteria. Use dot notation instead.

2.Move the subgroup's $map inside group's $map operator.

[
  {"$match":{
    "empId":"19107",
    "address.country":"AUS",
    "group.primaryGroup.primary":"Y",
    "group.subGroup.primarySubGroup.primary":"Y"
  }},
  {"$project":{
    "empId":1,
    "mobile":1,
    "address":{"$filter":{"input":"$address","as":"d","cond":{"$eq":["$$d.country","AUS"]}}},
    "group":{
      "$map":{
        "input":"$group",
        "as":"v",
        "in":{
          "groupId":"$$v.groupId",
          "primaryGroup":{"$filter":{"input":"$$v.primaryGroup","as":"vp","cond":{"$eq":["$$vp.primary","Y"]}}},
          "subGroup":{
            "$map":{
              "input":"$$v.subGroup",
              "as":"n",
              "in":{
                "subGroupId":"$$n.subGroupId",
                "primarySubGroup":{"$filter":{"input":"$$n.primarySubGroup","as":"mp","cond":{"$eq":["$$mp.primary","Y"]}}}
              }
            }
          }
        }
      }
    }
  }}
]
查看更多
登录 后发表回答