
- 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
Get fields from multiple sub-documents that match a condition in MongoDB?
To get fields from multiple sub-documents, use MongoDB aggregate with $unwind. Let us create a collection with documents −
> db.demo671.insertOne( ... { ... ... "details" : [ ... { ... "id" : "1" ... }, ... { ... CountryName:"US", ... "details1" : [ ... { ... "id" : "1" ... }, ... { ... "id" : "2" ... } ... ] ... }, ... { ... CountryName:"UK", ... "details1" : [ ... { ... "id" : "2" ... }, ... { ... "id" : "1" ... } ... ] ... }, ... { ... CountryName:"AUS", ... "details1" : [ ... { ... "id" : "1" ... } ... ] ... } ... ] ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5ea3e5d004263e90dac943e0") }
Display all documents from a collection with the help of find() method −
> db.demo671.find();
This will produce the following output −
{ "_id" : ObjectId("5ea3e5d004263e90dac943e0"), "details" : [ { "id" : "1" }, { "CountryName" : "US", "details1" : [ { "id" : "1" }, { "id" : "2" } ] }, { "CountryName" : "UK", "details1" : [ { "id" : "2" }, { "id" : "1" } ] }, { "CountryName" : "AUS", "details1" : [ { "id" : "1" } ] } ] }
Here is the query to get fields from multiple sub-documents that match a condition in MongoDB −
> db.demo671.aggregate([ ... ... {$unwind: '$details'}, ... ... {$match: {'details.details1.id': '1'}}, ... ... {$project: {_id: 0, Country: '$details.CountryName'}} ... ]).pretty()
This will produce the following output −
{ "Country" : "US" } { "Country" : "UK" } { "Country" : "AUS" }
- Related Articles
- MongoDB filter multiple sub-documents?
- Match MongoDB documents with fields not containing values in array?
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to match each element in a documents array to a condition?
- MongoDB query for exact match on multiple document fields
- Find records in MongoDB that does NOT match a condition?
- Finding highest value from sub-arrays in MongoDB documents?
- Filter sub documents by sub document in MongoDB?
- Sum MongoDB Sub-documents field?
- MongoDB query to get only specific fields in nested array documents?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- MongoDB query to match documents that contain an array field
- Match between fields in MongoDB aggregation framework?
- MongoDB query to get documents with multiple conditions set in $or?
- Get MongoDB documents that contains specific attributes in array

Advertisements