
- 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
Include all existing fields and add new fields to document in MongoDB?
You can achieve this with the help of $addFields operator. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.addFieldDemo.insertOne({"EmployeeId":101,"EmployeeName":"Larry","EmployeeDetails":{ "EmployeeSalary":65000,"EmployeeCity":"New York","Message":"Hi"}}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f654d8d10a061296a3c44") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.addFieldDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7f654d8d10a061296a3c44"), "EmployeeId" : 101, "EmployeeName" : "Larry", "EmployeeDetails" : { "EmployeeSalary" : 65000, "EmployeeCity" : "New York", "Message" : "Hi" } }
Here is the query to include all existing fields and add new fields to document in MongoDB −
> db.addFieldDemo.aggregate([ { "$addFields": { "EmployeeBasicSalary":"$EmployeeDetails.EmployeeSalary" } } ]).pretty();
The following is the output −
{ "_id" : ObjectId("5c7f654d8d10a061296a3c44"), "EmployeeId" : 101, "EmployeeName" : "Larry", "EmployeeDetails" : { "EmployeeSalary" : 65000, "EmployeeCity" : "New York", "Message" : "Hi" }, "EmployeeBasicSalary" : 65000 }
Look at the sample output, “EmployeeBasicSalary” has been added.
- Related Articles
- MongoDB query for fields in embedded document?
- MongoDB query with fields in the same document?
- What is the fastest way to update the whole document (all fields) in MongoDB?
- MongoDB - how can I access fields in a document?
- How to update record in MongoDB without replacing the existing fields?
- Sort array in MongoDB query and project all fields?
- MongoDB query for exact match on multiple document fields
- How to query a document in MongoDB comparing fields from an array?
- Get all fields names in a MongoDB collection?
- Group all documents with common fields in MongoDB?
- Select special fields rather than all in MongoDB
- How to find datatype of all the fields in MongoDB?
- How to retrieve all nested fields from MongoDB collection?
- How to project specific fields from a document inside an array in Mongodb?
- MongoDB query to add new array element in document

Advertisements