使用 $map 的 MongoDB 嵌套数组搜索 [英] MongoDB nested array search using $map

查看:39
本文介绍了使用 $map 的 MongoDB 嵌套数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含嵌套数组的集合.我需要根据以下条件获取数据:

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"

输入:

{
    "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"
                        }
                    ]
                }
            ]
        }
    ]
}

我需要如下输出:

{
    "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"]}}}}}}  }}]

我是 mongoDB 的新手.我尝试了以下方法(Spring 数据匹配和过滤嵌套数组)但我在嵌套数组获取中遇到了一些问题.例如:我需要比较 group 字段中存在的 $map 中的 primaryGroup 而不是 groupId.

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.单个条件不需要$elemMatch.请改用点表示法.

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

2.将子组的 $map 移动到组的 $map 运算符内.

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"]}}}
              }
            }
          }
        }
      }
    }
  }}
]

这篇关于使用 $map 的 MongoDB 嵌套数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆