mongodb mongo - 如何查询嵌套的 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10320633/
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:37:45  来源:igfitidea点击:

mongo - how to query a nested json

mongodb

提问by Blacksad

I am a complete mongo newbie. I am using mongo hub for mac. I need to query the for following json -

我是一个完整的 mongo 新手。我正在使用 mongo hub for mac。我需要查询以下 json -

{ "_id" : ObjectId( "abcd" ),
"className" : "com.myUser",
"reg" : 12345,
"test" : [ 
{ "className" : "com.abc",
  "testid" : "pqrs" } ] }

and find records where testid is pqrs. How would I go about doing that?

并查找 testid 为 pqrs 的记录。我该怎么做呢?

回答by Blacksad

You can type {'test.testid': 'pqrs'}in the query field of Mongo Hub.

您可以{'test.testid': 'pqrs'}在 Mongo Hub 的查询字段中输入。

回答by Aniket Thakur

Looks like testis an array. If you are expecting multiple values in array you can do -

看起来像是test一个数组。如果您期望数组中有多个值,您可以这样做 -

"test": { 
    "$elemMatch": {
        "testid": "pqrs",
    }
}