
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How can I search a collection to find a nested value in one of its documents in MongoDB?
For this, use double underscore( __) in find(). Let us first create a collection with documents −
> db.nestedDemo.insertOne({"Information":{"__StudentName":"John Smith"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f39125ddae1f53b621f0") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"John Doe"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f39e25ddae1f53b621f1") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"Chris Brown"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f3a625ddae1f53b621f2") }
Following is the query to display all documents from a collection with the help of find() method −
> db.nestedDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e06f39125ddae1f53b621f0"), "Information" : { "__StudentName" : "John Smith" } } { "_id" : ObjectId("5e06f39e25ddae1f53b621f1"), "Information" : { "__StudentName" : "John Doe" } } { "_id" : ObjectId("5e06f3a625ddae1f53b621f2"), "Information" : { "__StudentName" : "Chris Brown" } }
Here is the query to search a collection to find a nested value in one of its documents in MongoDB −
> db.nestedDemo.find({"Information.__StudentName":"John Doe"});
This will produce the following output −
{ "_id" : ObjectId("5e06f39e25ddae1f53b621f1"), "Information" : { "__StudentName" : "John Doe" } }
- Related Articles
- How can I aggregate nested documents in MongoDB?\n
- Find a value in lowercase from a MongoDB collection with documents
- How to find two random documents in a MongoDB collection of 6?
- How to sum the value of a key across all documents in a MongoDB collection?
- How can I rename a collection in MongoDB?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- Perform nested document value search in MongoDB?
- How to count the number of documents in a MongoDB collection?
- How to return documents of a collection without objectId in MongoDB?
- Evaluate one of more values from a MongoDB collection with documents
- Find value above a specific value in MongoDB documents?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How to Update multiple documents in a MongoDB collection using Java?
- How to delete all the documents from a collection in MongoDB?

Advertisements