
- 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
How to find a certain element in the MongoDB embedded document?
To find a certain element, use $project in MongoDB. Let us create a collection with documents −
> db.demo744.insertOne( ... { ... studentInformation: ... [ ... { ... studentName:"Robert", ... grade:"A" ... }, ... { ... studentName:"Bob", ... grade:"C" ... }, ... { ... studentName:"John", ... grade:"B" ... }, ... { ... studentName:"Sam", ... grade:"A" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ead928a57bb72a10bcf0684") }
Display all documents from a collection with the help of find() method −
> db.demo744.find();
This will produce the following output −
{ "_id" : ObjectId("5ead928a57bb72a10bcf0684"), "studentInformation" : [ { "studentName" : "Robert", "grade" : "A" }, { "studentName" : "Bob", "grade" : "C" }, { "studentName" : "John", "grade" : "B" }, { "studentName" : "Sam", "grade" : "A" } ] }
Following is the query to find a certain element in the embedded document −
> db.demo744.aggregate( ... { $unwind: '$studentInformation' }, ... { $match: {'studentInformation.grade':"A"}}, ... { $project: {"studentInformation.studentName": 1}} ... )
This will produce the following output −
{ "_id" : ObjectId("5ead928a57bb72a10bcf0684"), "studentInformation" : { "studentName" : "Robert" } } { "_id" : ObjectId("5ead928a57bb72a10bcf0684"), "studentInformation" : { "studentName" : "Sam" } }
- Related Articles
- How to get embedded data in a MongoDB document?
- Return specific MongoDB embedded document
- MongoDB query to return only embedded document?
- How to find if element exists in document - MongoDB?
- Implement MongoDB $push in embedded document array?
- MongoDB query for fields in embedded document?
- Increment a field in MongoDB document which is embedded?
- MongoDB - How to check for equality in collection and in embedded document?
- Add a field to an embedded document in an array in MongoDB?
- In MongoDB how do you use $set to update a nested value/embedded document?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- Filter query on array of embedded document with MongoDB?
- How to pull an array element (which is a document) in MongoDB?
- How to find only a single document satisfying the criteria in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?

Advertisements