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

查看:30
本文介绍了在双嵌套数组 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?

推荐答案

在最简单的意义上,这只是遵循 "dot notation" 的基本形式 由 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天全站免登陆