
- 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
Set condition in MongoDB nested document?
Let’s say we need to find a document with a value greater than specific value. For this, use dot notation in nested document and set the condition with $gt.
Let us see an example and create a collection with documents −
> db.demo688.insert( ... { ... information:{id:1,details:[ ... {otherDetails:{ ... values:75 ... } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo688.insert({ ... information: ... { ... id:2, ... details: ... [ ... {otherDetails:{ ... values:78 ... } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo688.find();
This will produce the following output −
{ "_id" : ObjectId("5ea57986a7e81adc6a0b3965"), "information" : { "id" : 1, "details" : [ { "otherDetails" : { "values" : 75 } } ] } } { "_id" : ObjectId("5ea5799ca7e81adc6a0b3966"), "information" : { "id" : 2, "details" : [ { "otherDetails" : { "values" : 78 } } ] } }
Following is the query to access MongoDB nested document −
> db.demo688.find({"information.details.otherDetails.values":{$gt:75}});
This will produce the following output −
{ "_id" : ObjectId("5ea5799ca7e81adc6a0b3966"), "information" : { "id" : 2, "details" : [ { "otherDetails" : { "values" : 78 } } ] } }
- Related Articles
- Updating nested document in MongoDB
- Perform nested document value search in MongoDB?
- MongoDB find() query for nested document?
- MongoDB query to update nested document
- In MongoDB how do you use $set to update a nested value/embedded document?
- MongoDB query to update the nested document?
- Set $gt condition in MongoDB $and
- Query nested array by more than one condition in MongoDB
- Make nested queries in MongoDB 4 to fetch a specific document
- Set filtering conditions for nested array in MongoDB
- Identify last document from MongoDB find() result set?
- How to remove an element from a doubly-nested array in a MongoDB document?
- Remove all except a single field from a nested document via projection in MongoDB
- MongoDB query to filter object where all elements from nested array match the condition
- Update a specific MongoDB document in array with $set and positional $ operator?

Advertisements