
- 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
Query a nested field within an array with MongoDB
To query a nested field within an array, use $elemMatch in MongoDB. Let us create a collection with documents −
> db.demo153.insertOne({"ClientDetails":[{"ClientName":"Chris","ClientProject":"Online Banking System"},{"ClientName":"David","ClientProject":"Online School Management"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e351957fdf09dd6d08539df") } > db.demo153.insertOne({"ClientDetails":[{"ClientName":"Carol","ClientProject":"Online Book System"},{"ClientName":"Mike","ClientProject":"Game Development"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3519c9fdf09dd6d08539e0") }
Display all documents from a collection with the help of find() method −
> db.demo153.find();
This will produce the following output −
{ "_id" : ObjectId("5e351957fdf09dd6d08539df"), "ClientDetails" : [ { "ClientName" : "Chris", "ClientProject" : "Online Banking System" }, { "ClientName" : "David", "ClientProject" : "Online School Management" } ] } { "_id" : ObjectId("5e3519c9fdf09dd6d08539e0"), "ClientDetails" : [ { "ClientName" : "Carol", "ClientProject" : "Online Book System" }, { "ClientName" : "Mike", "ClientProject" : "Game Development" } ] }
Following is how to query a nested field within an array −
> db.demo153.find({"ClientDetails": { "$elemMatch" : {"ClientProject": { "$in": ["Online Banking System"] } } } });
This will produce the following output −
{ "_id" : ObjectId("5e351957fdf09dd6d08539df"), "ClientDetails" : [ { "ClientName" : "Chris", "ClientProject" : "Online Banking System" }, { "ClientName" : "David", "ClientProject" : "Online School Management" } ] }
- Related Articles
- Query array of nested string with MongoDB?
- Update an array of strings nested within an array of objects in MongoDB
- Delete specific record from an array nested within another array in MongoDB?
- Deleting specific record from an array nested within another array in MongoDB
- Extract particular element in MongoDB within a Nested Array?
- Field selection within MongoDB query using dot notation?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to update array with another field?
- Creating an index on a nested MongoDB field?
- MongoDB query to get array of nested string?
- MongoDB query to match documents that contain an array field
- Replace an array field value with MongoDB?
- Query MongoDB for a nested search
- Query nested array by more than one condition in MongoDB

Advertisements