
- 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
MongoDB query to replace value with aggregation?
Use aggregate framework along with $literal operator. Let us first create a collection with documents −
> db.replaceValueDemo.insertOne( { _id : 100, "EmployeeName" :"Chris", "EmployeeOtherDetails": { "EmployeeDesignation" : "HR", "EmployeeAge":27 } } ); { "acknowledged" : true, "insertedId" : 100 } > db.replaceValueDemo.insertOne( { _id : 101, "EmployeeName" :"David", "EmployeeOtherDetails": { "EmployeeDesignation" : "Tester", "EmployeeAge":26 } } ); { "acknowledged" : true, "insertedId" : 101 }
Following is the query to display all documents from a collection with the help of find() method −
> db.replaceValueDemo.find().pretty();
This will produce the following output −
{ "_id" : 100, "EmployeeName" : "Chris", "EmployeeOtherDetails" : { "EmployeeDesignation" : "HR", "EmployeeAge" : 27 } } { "_id" : 101, "EmployeeName" : "David", "EmployeeOtherDetails" : { "EmployeeDesignation" : "Tester", "EmployeeAge" : 26 } }
Following is the query to replace a value −
> db.replaceValueDemo.aggregate([{ "$project": { "_id": 1, "EmployeeOtherDetails": { EmployeeAge: 1, EmployeeDesignation : { $literal: "Developer" } } } }]);
This will produce the following output −
{ "_id" : 100, "EmployeeOtherDetails" : { "EmployeeAge" : 27, "EmployeeDesignation" : "Developer" } } { "_id" : 101, "EmployeeOtherDetails" : { "EmployeeAge" : 26, "EmployeeDesignation" : "Developer" } }
- Related Articles
- Replace value with a string literal during MongoDB aggregation operation
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB aggregation framework with group query example?
- MongoDB query to replace value in an array?
- Does aggregation query with $match works in MongoDB?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to group several fields using aggregation framework?
- Replace an array field value with MongoDB?
- Sort and Group in one MongoDB aggregation query?
- MongoDB query to get average in aggregation of array element?
- Make MongoDB replace single array value with string?
- MongoDB aggregation with multiple keys
- How to use $ifNull with MongoDB aggregation?
- How to group nested fields in MongoDB aggregation with count value in array?

Advertisements