
- 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 filtering conditions for nested array in MongoDB
To set filtering conditions, use $filter and $cond in MongoDB aggregate(). The $filter selects a subset of an array to return based on the specified condition. Let us create a collection with documents −
> db.demo725.insertOne( ... { ... ... "details": { ... ... "userMessages": [ ... { ... "Messages": [ ... { "Message": "Hello" }, ... { "Message": "How" }, ... { "Message": "are" } ... ] ... ... }, ... { ... "Messages": [ ... { "Message": "Good" }, ... { "Message": "Bye" } ... ... ] ... }, ... { ... "Messages": [ ... { "Message": "Hello" }, ... { "Message": "Bye" } ... ... ] ... ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eab16cd43417811278f5893") }
Display all documents from a collection with the help of find() method −
> db.demo725.find();
This will produce the following output −
{ "_id" : ObjectId("5eab16cd43417811278f5893"), "details" : { "userMessages" : [ { "Messages" : [ { "Message" : "Hello" }, { "Message" : "How" }, { "Message" : "are" } ] }, { "Messages" : [ { "Message" : "Good" }, { "Message" : "Bye" } ] }, { "Messages" : [ { "Message" : "Hello" }, { "Message" : "Bye" } ] } ] } }
Following is the query to set filtering conditions for nested array −
> db.demo725.aggregate([ ... { ... $addFields: { ... "details.userMessages": { ... $filter: { ... input: "$details.userMessages", ... as: "out", ... cond: { ... $anyElementTrue: { ... $map: { ... input: "$$out.Messages", ... in: { $gte: [ { $indexOfBytes: [ "$$this.Message", "Hello" ] }, 0 ] } ... } ... } ... } ... } ... } ... } ... } ... ]).pretty()
This will produce the following output −
{ "_id" : ObjectId("5eab16cd43417811278f5893"), "details" : { "userMessages" : [ { "Messages" : [ { "Message" : "Hello" }, { "Message" : "How" }, { "Message" : "are" } ] }, { "Messages" : [ { "Message" : "Hello" }, { "Message" : "Bye" } ] } ] } }
- Related Articles
- Set condition in MongoDB nested document?
- MongoDB $push in nested array?
- Aggregation in MongoDB for nested documents?
- Clearing items in a nested MongoDB array?
- MongoDB Increment value inside nested array?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- How to create double nested array in MongoDB?
- Retrieve values from nested JSON array in MongoDB?
- Query MongoDB for a nested search
- MongoDB find() query for nested document?
- Querying on an array of objects for specific nested documents with MongoDB?
- Increment MongoDB value inside a nested array
- Query array of nested string with MongoDB?
- How to update array with multiple conditions in MongoDB

Advertisements