
- 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
MongoDB - Query embedded documents?
To query embedded documents in MongoDB, use aggregate(). Let us create a collection with documents −
> db.demo705.insertOne( ... { ... _id:101, ... "Information": ... [ ... { ... "StudentName":"Chris", ... "StudentAge":21 ... }, ... { ... "StudentName":"David", ... "StudentAge":23 ... }, ... { ... "StudentName":"Bob", ... "StudentAge":20 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo705.find();
This will produce the following output −
{ "_id" : 101, "Information" : [ { "StudentName" : "Chris", "StudentAge" : 21 }, { "StudentName" : "David", "StudentAge" : 23 }, { "StudentName" : "Bob", "StudentAge" : 20 } ] }
Following is how to query embedded documents in MongoDB −
> db.demo705.aggregate( ... { $unwind: '$Information' }, ... { $match: {'Information.StudentAge': {$gte: 21}}}, ... { $project: {Information: 1}} ... )
This will produce the following output −
{ "_id" : 101, "Information" : { "StudentName" : "Chris", "StudentAge" : 21 } } { "_id" : 101, "Information" : { "StudentName" : "David", "StudentAge" : 23 } }
- Related Articles
- Query an array of embedded documents in MongoDB and push another?
- Check for existing documents/embedded documents in MongoDB
- Updating Nested Embedded Documents in MongoDB?
- MongoDB query to skip documents
- MongoDB query for fields in embedded document?
- MongoDB query to return only embedded document?
- Querying array of Embedded Documents in MongoDB based on Range?
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Appending an entry in one to many embedded documents with MongoDB
- MongoDB query to skip n first documents?
- Filter documents in MongoDB using simple query?
- Filter query on array of embedded document with MongoDB?
- MongoDB query to get distinct FirstName values from documents

Advertisements