- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update MongoDB field using value of another field?
You can use aggregate function to update MongoDB field using the value of another field. Here, we will create two collections:
name
studentInformation
<name> Collection
The query to create first collection with documents is as follows:
> db.name.insert({"FirstName":"John","LastName":"Smith"}); WriteResult({ "nInserted" : 1 })
Now you can display all documents from the collection with the help of find() method. The query is as follows:
> db.name.find().pretty();
The following is the output that displays the collection “name” documents:
{ "_id" : ObjectId("5c6c00dd68174aae23f5ef55"), "FirstName" : "John", "LastName" : "Smith" }
<studentInformation> Collection
The query to create second collection with documents is as follows:
> db.studentInformation.insert({"StudentFirstName":"Carol","StudentLastName":"Taylor"}); WriteResult({ "nInserted" : 1 })
Now you can display all documents from the collection with the help of find() method. The query is as follows:
> db.studentInformation.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6c013068174aae23f5ef56"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" }
Now, let us update collection “name” with another collection “studentInformation”. The query is as follows:
> db.studentInformation.aggregate( [ {"$addFields":{"FullName":{"$concat":["$StudentFirstName"," ","$StudentLastName"]} }}, {"$out":"name"} ] );
Now you can check the documents of the collection “name”. The query is as follows:
> db.name.find().pretty();
The following is the output that displays that we have successfully updated the fields:
{ "_id" : ObjectId("5c6c013068174aae23f5ef56"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor", "FullName" : "Carol Taylor" }
- Related Articles
- MongoDB query to update array with another field?
- Update _id field in MongoDB
- Update salary field value with 10 percent of each employee in MongoDB
- Convert a field to an array using MongoDB update operation?
- How to update _id field in MongoDB?
- How to run MongoDB query to update only a specific field value?
- Convert a field to an array using update operation in MongoDB
- How to derive value of a field from another field in MySQL?
- Want to update inner field in a MongoDB
- Update field in exact element array in MongoDB?
- Conditional update depending on field matched in MongoDB
- Sorting field value (FirstName) for MongoDB?
- Find values group by another field in MongoDB?
- MongoDB query to create new field and count set the count of another field in it?
- How to update the value of a field in a request using Rest Assured?
