
- 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 would I filter out sub documents in MongoDB?
To filter out sub documents, use MongoDB aggregate and in that, use $unwind. Let us create a collection with documents −
> db.demo662.insertOne( ... { ... "details":[ ... { ... Name:"Chris", ... Marks:35 ... }, ... { ... Name:"Bob", ... Marks:45 ... }, ... { ... Name:"David", ... Marks:30 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea1b2be24113ea5458c7d04") }
Display all documents from a collection with the help of find() method −
> db.demo662.find();
This will produce the following output −
{ "_id" : ObjectId("5ea1b2be24113ea5458c7d04"), "details" : [ { "Name" : "Chris", "Marks" : 35 }, { "Name" : "Bob", "Marks" : 45 }, { "Name" : "David", "Marks" : 30 } ] }
Following is the query to filter out sub documents −
> db.demo662.aggregate({$unwind:"$details"},{$match:{"details.Marks":{$gt:40}}})
This will produce the following output −
{ "_id" : ObjectId("5ea1b2be24113ea5458c7d04"), "details" : { "Name" : "Bob", "Marks" : 45 } }
- Related Articles
- MongoDB filter multiple sub-documents?
- Filter sub documents by sub document in MongoDB?
- Sum MongoDB Sub-documents field?
- Filter documents in MongoDB using simple query?
- How to fire find query on sub-documents in MongoDB?
- How to filter documents based on an array in MongoDB?
- Finding highest value from sub-arrays in MongoDB documents?
- Filter documents in MongoDB if all keys exist as fields?
- How can I aggregate nested documents in MongoDB?\n
- Get fields from multiple sub-documents that match a condition in MongoDB?
- How do I return a document with filtered sub-documents using Mongo?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How can I rename a field for all documents in MongoDB?
- Can I retrieve multiple documents from MongoDB by id?
- How to update all documents in MongoDB?

Advertisements