
- 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
Querying on an array of objects for specific nested documents with MongoDB?
To query on an array of objects for nested documents, use find(). Let us create a collection with documents −
> db.demo763.insertOne( ... { ... _id:1, ... CountryName:"US", ... "studentInformation": [ ... { ... StudentName:"Chris", ... }, ... { ... StudentName:"David", ... StudentAge:22 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo763.find();
This will produce the following output −
{ "_id" : 1, "CountryName" : "US", "studentInformation" : [ { "StudentName" : "Chris" }, { "StudentName" : "David", "StudentAge" : 22 } ] }
Following is how to query an array of objects to fetch specific nested documents −
> db.demo763.find({}, ... { ... studentInformation: { ... $elemMatch: { ... StudentAge: { ... $exists: true ... } ... } ... } ... })
This will produce the following output −
{ "_id" : 1, "studentInformation" : [ { "StudentName" : "David", "StudentAge" : 22 } ] }
- Related Articles
- Querying array of Embedded Documents in MongoDB based on Range?
- Update objects in a MongoDB documents array (nested updating)?
- MongoDB query to get only specific fields in nested array documents?
- Find MongoDB documents where all objects in array have specific value?
- Update an array of strings nested within an array of objects in MongoDB
- Aggregation in MongoDB for nested documents?
- Find in MongoDB documents with filled nested array and reshape the documents result
- Querying array elements with MongoDB?
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Querying from part of object in an array with MongoDB
- Querying an array of arrays in MongoDB?
- MongoDB projection on specific nested properties?
- Find MongoDB documents where elements of an array have a specific value?
- Deleting specific record from an array nested within another array in MongoDB
- Delete specific record from an array nested within another array in MongoDB?

Advertisements