
- 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
Extract particular element in MongoDB within a Nested Array?
To extract the particular element in MongoDB, you can use $elemMatch operator. Let us first create a collection with documents −
> db.particularElementDemo.insertOne( { "GroupId" :"Group-1", "UserDetails" : [ { "UserName" : "John", "UserOtherDetails" : [ { "UserEmailId" : "John123@gmail.com", "UserFriendName" : [ { "Name" : "Chris" } ] }, { "UserEmailId" : "John22@hotmail.com", "UserFriendName" : [ { "Name" : "Robert" } ] } ] } ] } ); { "acknowledged" : true, "insertedId" : 100 } > db.particularElementDemo.find().pretty(); { "_id" : 100, "GroupId" : "Group-1", "UserDetails" : [ { "UserName" : "John", "UserOtherDetails" : [ { "UserEmailId" : "John123@gmail.com", "UserFriendName" : [ { "Name" : "Chris" } ] }, { "UserEmailId" : "John22@hotmail.com", "UserFriendName" : [ { "Name" : "Robert" } ] } ] } ] }
Display all documents from a collection with the help of find() method −
> db.particularElementDemo.find().pretty();
This will produce the following output −
{ "_id" : 100, "GroupId" : "Group-1", "UserDetails" : [ { "UserName" : "John", "UserOtherDetails" : [ { "UserEmailId" : "John123@gmail.com", "UserFriendName" : [ { "Name" : "Chris" } ] }, { "UserEmailId" : "John22@hotmail.com", "UserFriendName" : [ { "Name" : "Robert" } ] } ] } ] }
Following is the query to extract particular element in MongoDB in nested arrays −
> db.particularElementDemo.find( { 'UserDetails':{ $elemMatch:{ 'UserOtherDetails':{ $elemMatch:{ 'UserFriendName':{ $elemMatch: {"Name" : "Robert" } } } } } } },{"UserDetails.UserOtherDetails.UserFriendName.Name":1} );
This will produce the following output −
{ "_id" : 100, "UserDetails" : [ { "UserOtherDetails" : [ { "UserFriendName" : [ { "Name" : "Chris" } ] }, { "UserFriendName" : [ { "Name" : "Robert" } ] } ] } ] }
- Related Articles
- Extract a particular element from a nested array in MongoDB
- Query a nested field within an array with MongoDB
- Deleting specific record from an array nested within another array in MongoDB
- Delete specific record from an array nested within another array in MongoDB?
- Update an array of strings nested within an array of objects in MongoDB
- How to get a particular element from MongoDB array?
- How to remove an element from a doubly-nested array in a MongoDB document?
- MongoDB $push in nested array?
- Clearing items in a nested MongoDB array?
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- Increment MongoDB value inside a nested array
- Update objects in a MongoDB documents array (nested updating)?
- MongoDB Increment value inside nested array?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?

Advertisements