
- 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
Retrieving an embedded object as a document via the aggregation framework in MongoDB?
To retrieve an embedded object as a document, use the aggregation $replaceRoot. Let us first create a collection with documents −
> db.embeddedObjectDemo.insertOne( { _id: new ObjectId(), "UserDetails": { "UserName": "John", "UserAge": 24, "UserEmailId": "John22@gmail.com" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ced580fef71edecf6a1f693") } > db.embeddedObjectDemo.insertOne( { _id: new ObjectId(), "UserDetails": { "UserName": "Carol", "UserAge": 26, "UserEmailId": "Carol123@gmail.com" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ced5828ef71edecf6a1f694") }
Following is the query to display all documents from a collection with the help of find() method −
> db.embeddedObjectDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ced580fef71edecf6a1f693"), "UserDetails" : { "UserName" : "John", "UserAge" : 24, "UserEmailId" : "John22@gmail.com" } } { "_id" : ObjectId("5ced5828ef71edecf6a1f694"), "UserDetails" : { "UserName" : "Carol", "UserAge" : 26, "UserEmailId" : "Carol123@gmail.com" } }
Following is the query to retrieve an embedded object as a document via the aggregation framework in MongoDB −
> db.embeddedObjectDemo.aggregate( [ { $replaceRoot: { newRoot: "$UserDetails" } } ] );
This will produce the following output −
{ "UserName" : "John", "UserAge" : 24, "UserEmailId" : "John22@gmail.com" } { "UserName" : "Carol", "UserAge" : 26, "UserEmailId" : "Carol123@gmail.com" }
- Related Articles
- Retrieving the first document in a MongoDB collection?
- Add a field to an embedded document in an array in MongoDB?
- Match between fields in MongoDB aggregation framework?
- Return specific MongoDB embedded document
- Get the average of an entire field using the aggregation framework in MongoDB?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework match OR is possible?
- MongoDB aggregation framework with group query example?
- MongoDB query for fields in embedded document?
- Implement MongoDB $push in embedded document array?
- Increment a field in MongoDB document which is embedded?
- How to get embedded data in a MongoDB document?
- How to find a certain element in the MongoDB embedded document?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to return only embedded document?

Advertisements