mongodb Mongo db - 查询嵌套数组和对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11638224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:45:56  来源:igfitidea点击:

Mongo db - Querying nested array and objects

mongodbmongodb-query

提问by karel lahmy

I have the following document structure:

我有以下文档结构:

{
   "_id":"12345",
   "value":{
      "T":0,
      "v":[
         {
            "name":"JW",
            "cost":100
         }
      ]
   }
}

How do I query the name key? I tried the dot notation but without luck (I think it works for only two levels)

如何查询名称键?我尝试了点符号但没有运气(我认为它只适用于两个级别)

回答by JohnnyHK

It's not clear exactly what you tried, but this should work to find the above doc by name:

目前尚不清楚您尝试了什么,但这应该可以通过以下方式找到上述文档name

db.collection.find({ "value.v.name": "JW" })

Reference

参考

回答by neftedollar

You should use $elemMatchoperator:

您应该使用$elemMatch运算符:

db.collection.find({
    'value.v': { 
        $elemMatch: {
            name: 'JW', // "name == 'JW'"
            cost : 100 //if you need "&& cost == 100"
        }
    }
});

Mongo docs

Mongo 文档