
- 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 to filter object where all elements from nested array match the condition
For this, use aggregate(). Let us first create a collection with documents −
> db.demo418.insertOne( ... { ... "details":[ ... { ... "CountryName":"US", ... "Marks":45 ... }, ... { ... "CountryName":"US", ... "Marks":56 ... }, ... ... ], ... "Name":"John" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e724324b912067e57771ae6") } > db.demo418.insertOne( ... { ... "details":[ ... { ... "CountryName":"US", ... "Marks":78 ... }, ... { ... "CountryName":"UK", ... "Marks":97 ... }, ... ... ], ... "Name":"Mike" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e724325b912067e57771ae7") }
Display all documents from a collection with the help of find() method −
> db.demo418.find();
This will produce the following output −
{ "_id" : ObjectId("5e724324b912067e57771ae6"), "details" : [ { "CountryName" : "US", "Marks" : 45 }, { "CountryName" : "US", "Marks" : 56 } ], "Name" : "John" } { "_id" : ObjectId("5e724325b912067e57771ae7"), "details" : [ { "CountryName" : "US", "Marks" : 78 }, { "CountryName" : "UK", "Marks" : 97 } ], "Name" : "Mike" }
Following is the query to filter object where all elements from nested array match the condition −
> db.demo418.aggregate([ ... {$unwind:"$details"}, ... { $group: { ... _id: '$_id', ... details : { $first: '$details' }, ... alldetails: { $sum: 1 }, ... alldetailsmatch: { $sum: { $cond: [ { $eq: [ '$details.CountryName', "US" ] }, 1, 0 ] } }, ... Name: { $first: '$Name' } ... }}, ... { $project: { ... _id: 1, ... Name: 1, ... details: 1, ... arrayValue: { $cond: [ { $eq: [ '$alldetails', '$alldetailsmatch' ] }, 1, 0 ] } ... }}, ... { $match: { 'arrayValue' : 1 } } ... ])
This will produce the following output −
{ "_id" : ObjectId("5e724324b912067e57771ae6"), "details" : { "CountryName" : "US", "Marks" : 45 }, "Name" : "John", "arrayValue" : 1 }
- Related Articles
- MongoDB query to filter by several array elements?
- MongoDB query where all array items are less than a specified condition?
- MongoDB query where all array items are greater than a specified condition?
- Query nested array by more than one condition in MongoDB
- MongoDB query to match each element in a documents array to a condition?
- How to pull all elements from an array in MongoDB without any condition?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to match and remove element from an array?
- Group query upon nested object in MongoDB?
- How to filter array elements in MongoDB?
- MongoDB query to get array of nested string?
- MongoDB query to remove array elements from a document?
- How to filter an array from all elements of another array – JavaScript?
- Query array of nested string with MongoDB?

Advertisements