GnUfTw
GnUfTw

Reputation: 357

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

Screenshot of database in 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. (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. I would like to query the tests collection and return any document that contains a particular Name (such as EditMessagesSettings in this case). 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.

Edit: if it is any constellation, if I type in db.getCollection('tests').find({Obj}), I get an auto-complete option of ObjectId(.

Upvotes: 2

Views: 2157

Answers (2)

ittv
ittv

Reputation: 58

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.

So your query should look like:

db.getCollection('tests').find({"Data.__value.Name":"EditMessagesSettings"})

Upvotes: 4

Luke Villanueva
Luke Villanueva

Reputation: 2070

Try

db.getCollection('tests').find({"Data._value.Name":"EditMessagesSettings"})

Upvotes: 0

Related Questions