
- 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 to filter array in subdocument with MongoDB?
You can use aggregate and unwind the array list before applying match. To understand the above concept, let us create a collection with documents. The query to create a collection with document is as follows:
> db.filterArray.insertOne( { "L": [{ "N":1 }, { "N":2 } , { "N":3 }, { "N":4 }, { "N":5 } ]});
The following is visible after running the above query:
{ "acknowledged" : true, "insertedId" : ObjectId("5c6d63f2734e98fc0a434aeb") }
Display document from a collection with the help of find() method. The query is as follows:
> db.filterArray.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6d63f2734e98fc0a434aeb"), "L" : [ { "N" : 1 }, { "N" : 2 }, { "N" : 3 }, { "N" : 4 }, { "N" : 5 } ] }
Here is the query to filter array in subdocument with MongoDB. We have used $gt to get the fields greater than 3:
> db.filterArray.aggregate({ $match:{_id:ObjectId("5c6d63f2734e98fc0a434aeb")}}, { $unwind:'$L'}, {$match:{'L.N':{$gt:3}}}, {$group:{_id:'$_id',subDocument:{$push:'$L.N'}}}).pretty();
The following is the output:
{ "_id" : ObjectId("5c6d63f2734e98fc0a434aeb"), "subDocument" : [ 4, 5 ] }
- Related Articles
- How to filter array elements in MongoDB?
- How to select a specific subdocument in MongoDB?
- How to find specific array elements in MongoDB document with query and filter with range?
- Finding a specific item in subdocument with MongoDB?
- How to filter documents based on an array in MongoDB?
- MongoDB query to find and return subdocument with criteria?
- How to sort, select and query subdocument in MongoDB?
- Filter query on array of embedded document with MongoDB?
- Sort by subdocument in MongoDB
- MongoDB to sort by subdocument match?
- How to compare two fields in aggregation filter with MongoDB?
- Filter by several array elements in MongoDB?
- MongoDB query on nth element (variable index) of subdocument array
- MongoDB query to filter by several array elements?
- Display MongoDB with document and subdocument example and update

Advertisements