在双嵌套数组MongoDB中查找 [英] Find in Double Nested Array MongoDB

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

问题描述

我在mongodb中有此收藏集

I have this Collection in mongodb

{
"_id" : "777",
"someKey" : "someValue",
"someArray" : [
    {
        "name" : "name1",
        "someNestedArray" : [
            {
                "name" : "value"
            },
            {
                "name" : "delete me"
            }
        ]
    }
  ]
}

我想找到基于someArray.someNestedArray.name的文档 但我找不到任何有用的链接有关更新嵌套数组的所有搜索结果 我正在尝试这个,但是什么也没返回

I want to find document based on someArray.someNestedArray.name but i can't find any useful link all search result about update nested array i am trying this but return nothing

db.mycollection.find({"someArray.$.someNestedArray":{"$elemMatch":{"name":"1"}}})
db.mycollection.find({"someArray.$.someNestedArray.$.name":"1"})

还有其他事情

如何在双嵌套数组mongodb中按元素查找?

how can i find by element in double nested array mongodb?

推荐答案

从最简单的意义上讲,这只是遵循点表示法"的基本形式(由MongoDB使用).不管内部数组成员位于哪个数组成员中,只要它与一个值匹配,该方法都将起作用:

In the simplest sense this just follows the basic form of "dot notation" as used by MongoDB. That will work regardless of which array member the inner array member is in, as long as it matches a value:

db.mycollection.find({
    "someArray.someNestedArray.name": "value"
})

这对于单个字段"值很好,对于匹配多个字段,您可以使用 $elemMatch :

That is fine for a "single field" value, for matching multiple-fields you would use $elemMatch:

db.mycollection.find({
    "someArray": { 
        "$elemMatch": {
            "name": "name1",
            "someNestedArray": {
                "$elemMatch": {
                    "name": "value",
                    "otherField": 1
                }
            }
        }
    }
})

与文档匹配,该文档可能包含某些内容,且该路径"中的字段与值匹配.如果要匹配和过滤"结果,以便仅返回匹配的元素,则使用位置运算符投影

That matches the document which would contain something with a a field at that "path" matching the value. If you intended to "match and filter" the result so only the matched element was returned, this is not possible with the positional operator projection, as quoted:

嵌套数组

位置$运算符不能用于遍历一个以上数组的查询,例如遍历嵌套在其他数组中的数组的查询,因为$占位符的替换是单个值

The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value

现代MongoDB

我们可以通过应用 $filter $map 此处.确实需要$map,因为内部"数组可能会由于过滤"而改变,并且外部"数组当然与内部"数组中的所有元素都剥离时的条件不匹配.

Modern MongoDB

We can do this by applying $filter and $map here. The $map is really needed because the "inner" array can change as a result of the "filtering", and the "outer" array of course does not match the conditions when the "inner" was stripped of all elements.

再次以实际在每个数组中具有多个属性进行匹配的示例为例:

Again following the example of actually having multiple properties to match within each array:

db.mycollection.aggregate([
  { "$match": {
    "someArray": {
      "$elemMatch": {
         "name": "name1",
         "someNestedArray": {
           "$elemMatch": {
             "name": "value",
             "otherField": 1
           }
         }
       }
    }
  }},
  { "$addFields": {
    "someArray": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$someArray",
            "as": "sa",
            "in": {
              "name": "$$sa.name",
              "someNestedArray": {
                "$filter": {
                  "input": "$$sa.someNestedArray",
                  "as": "sn",
                  "cond": {
                    "$and": [
                      { "$eq": [ "$$sn.name", "value" ] },
                      { "$eq": [ "$$sn.otherField", 1 ] }
                    ]
                  }
                }
              }             
            }
          },
        },
        "as": "sa",
        "cond": {
          "$and": [
            { "$eq": [ "$$sa.name", "name1" ] },
            { "$gt": [ { "$size": "$$sa.someNestedArray" }, 0 ] }
          ]
        }
      }
    }
  }}
])

因此,在外部"数组上, $filter 实际查看内部"数组的 $size 它本身是经过过滤"的,因此,当整个内部数组确实符合条件时,您就可以拒绝那些结果.

Therefore on the "outer" array the $filter actually looks at the $size of the "inner" array after it was "filtered" itself, so you can reject those results when the whole inner array does in fact match noting.

为了仅投影"匹配的元素,您需要 .aggregate() 方法:

In order to "project" only the matched element, you need the .aggregate() method:

db.mycollection.aggregate([
    // Match possible documents
    { "$match": {
        "someArray.someNestedArray.name": "value"
    }},

    // Unwind each array
    { "$unwind": "$someArray" },
    { "$unwind": "$someArray.someNestedArray" },

    // Filter just the matching elements
    { "$match": {
        "someArray.someNestedArray.name": "value"
    }},

    // Group to inner array
    { "$group": {
        "_id": { 
            "_id": "$_id", 
            "name": "$someArray.name"
        },
        "someKey": { "$first": "$someKey" },
        "someNestedArray": { "$push": "$someArray.someNestedArray" }
    }},

    // Group to outer array
    { "$group": {
        "_id": "$_id._id",
        "someKey": { "$first": "$someKey" },
        "someArray": { "$push": {
            "name": "$_id.name",
            "someNestedArray": "$someNestedArray"
        }}
    }} 
])

这使您可以过滤"嵌套数组中的匹配项,以查找文档中的一个或多个结果.

That allows you to "filter" the matches in nested arrays for one or more results within the document.

这篇关于在双嵌套数组MongoDB中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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