
- 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
Filter query on array of embedded document with MongoDB?
For this, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo736.insertOne( ... { ... "_id": "101", ... "details1": [ ... { ... "details2": [ ... { ... "details3": { ... "Name": "John" ... } ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "101" } > db.demo736.insertOne( ... { ... "_id": "102", ... "details1": [ ... { ... "details2": [ ... { ... "details3": { ... "Name": "Robert" ... } ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "102" }
Display all documents from a collection with the help of find() method −
> db.demo736.find();
This will produce the following output −
{ "_id" : "101", "details1" : [ { "details2" : [ { "details3" : { "Name" : "John" } } ] } ] } { "_id" : "102", "details1" : [ { "details2" : [ { "details3" : { "Name" : "Robert" } } ] } ] }
Following is the query to filter array of embedded documents (3 levels) in MongoDB −
> db.demo736.aggregate([ ... { ... "$unwind": "$details1" ... }, ... { ... "$unwind": "$details1.details2" ... }, ... { ... "$match": { ... "details1.details2.details3.Name": "Robert" ... } ... }, ... { ... $project: { ... _id: 0, ... Name: "$details1.details2.details3", ... } ... } ... ])
This will produce the following output −
{ "Name" : { "Name" : "Robert" } }
- Related Articles
- MongoDB query for fields in embedded document?
- MongoDB query to return only embedded document?
- How to find specific array elements in MongoDB document with query and filter with range?
- Implement MongoDB $push in embedded document array?
- Return specific MongoDB embedded document
- MongoDB - Query embedded documents?
- Query an array of embedded documents in MongoDB and push another?
- MongoDB query to filter by several array elements?
- How to filter a query on specific date format with MongoDB?
- MongoDB query to push document into an array
- Add a field to an embedded document in an array in MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- MongoDB query to remove array elements from a document?
- MongoDB query to add new array element in document
- Query on the last object of an array with MongoDB

Advertisements