[英]How can I search a collection to find a nested value in one of its documents in MongoDB
In the screenshot above you can see that I have open a document named (1) ObjectId(572b...ec7a) from my tests collection in my mongo database. 在上面的屏幕截图中,您可以看到我从mongo数据库的测试集合中打开了一个名为(1)ObjectId(572b ... ec7a)的文档。 (the tests collection is full of documents like this) Near the bottom of the screenshot you can see there is a field named Name that is type string with a value of EditMessagesSettings .
( 测试集合中充满了这样的文档)在屏幕快照的底部,您可以看到一个名为Name的字段,该字段的类型为string,其值为EditMessagesSettings 。 I would like to query the tests collection and return any document that contains a particular Name (such as EditMessagesSettings in this case).
我想查询测试集合并返回任何包含特定名称的文档(在这种情况下,例如EditMessagesSettings )。 How can I do this?
我怎样才能做到这一点?
Here are some of the queries I have attempted without success, getting an return message like Fetched 0 record(s) in 4ms . 这是我尝试但未成功执行的一些查询, 在4ms中得到了诸如Fetched 0 record(s)之类的返回消息。
Edit: if it is any constellation, if I type in db.getCollection('tests').find({Obj}) , I get an auto-complete option of ObjectId( . 编辑:如果是任何星座,如果我输入db.getCollection('tests')。find({Obj}) ,我将获得ObjectId( )的自动完成选项。
I can see where the problem is. 我可以看到问题出在哪里。 You need to use two underscores instead of one.
您需要使用两个下划线而不是一个。 See the example below:
请参阅以下示例:
_value vs. the correct one __value. _value与正确的一个__value。
So your query should look like: 因此,您的查询应如下所示:
db.getCollection('tests').find({"Data.__value.Name":"EditMessagesSettings"})
尝试
db.getCollection('tests').find({"Data._value.Name":"EditMessagesSettings"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.