简体   繁体   English

如何在MongoDB中搜索集合以在其文档之一中找到嵌套值

[英]How can I search a collection to find a nested value in one of its documents in MongoDB

Robomongo中数据库的屏幕截图

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)之类的返回消息。

  • db.getCollection('tests').find({Name:"EditMessagesSettings"}) db.getCollection( '测试')找到。({名称: “EditMessagesSettings”})
  • db.getCollection('tests').find({Name:{$regex:"Settings"}}) 。db.getCollection( '测试')找到({名称:{$正则表达式: “设置”}})

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.

相关问题 如何在一个集合中找到另一个集合中的文档未引用的 MongoDB 文档? - How to find MongoDB documents in one collection not referenced by documents in another collection? 如何使用变量作为集合在MongoDB中查找文档? - How can I find documents in MongoDB using a variable as collection? MongoDB:在其中一个字段中查找包含特定值的文档 - MongoDB: find documents that contain a certain value in one of its fields 如何 mongodb 搜索嵌套文档并找到完全匹配的字段 - how to mongodb search nested documents and find exact match field 我应该如何为嵌套文档建模MongoDB集合? - How should I model my MongoDB collection for nested documents? 如何找到 mongodb 集合中包含字段/值对的所有文档? - How do I find all documents in mongodb collection that contain field/value pair? 如何在MongoDB中找到类似的文档? - How can I find similar documents in MongoDB? 在 MongoDB 中,使用 Java 我如何查询以在数据库或集合中查找超过 15 MB 存储大小的文档 - In MongoDB, using Java how can I query to find documents exceeding 15 MB storage size in a database or collection MongoDB - 如何找到其他集合中未被文档引用的所有文档 - MongoDB - how can I find all documents that aren't referenced by a document from another collection MongoDB/Mongoose:如何计算一个集合中的文档并根据 id 将其添加到另一个集合中? - MongoDB/Mongoose: How can I count documents in one collection and add it to another based on id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM