How to update a field using a value from another field in MongoDB? In this guide, we will discuss updating field functionality in MongoDB.
For changing a field using the data from a different field, MongoDB provides $set, $project, and $out. Each of these methods has its advantages and disadvantages, depending on the specific use case.
<a id="h-1-methods-to-update-fields-using-the-value-from-other-fields-in-mongodb"></a>
1. Methods to update fields using the value from other fields in MongoDB
Here, we will dive into some methods with the example implementation. Before using those approaches, let’s have a look at the following collection. Here, we have a collection of documents representing employee's information, and each document has the following fields:
#Create Collection
db.Employees.insertMany([
{
"_id": 1,
"name" : "Sam" ,
"father_name": "David",
"salary": 5600,
"bonus": 100,
"email": "sam12@gmail.com"
},
{
"_id": 2,
"name" : "Emily" ,
"father_name": "Paul",
"salary": 3000,
"bonus": 500,
"email": "emily22@gmail.com"
},
{
"_id": 3,
"name" : "Bella" ,
"father_name": "Kim",
"salary": 2000,
"bonus": 400,
"email": "bella89@gmail.com"
}
])
<a id="h-1-1-update-field-from-another-field-using-set-operator"></a>
1.1 Update Field From Another Field Using $set operator
The $set operator is used to updating the value of a field in a document. To update a field using the value of another field in MongoDB, we can use the $set operator with an expression that references the value of the other field. The following update operation will update the email field with the value of the $name field where “_id” is “1” by using the $set operator.
#Using Set Operator
db.Employees.updateMany(
{_id: 1},
{ $set: { email: "$name" } }
)
The output displayed the updated document below.
Now, to view the document with the modified fields, run the following query.
#View Modified Fields
db.Employees.find().pretty();
Yields below output.
<a id="h-1-2-using-the-project-operator"></a>
1.2 Using the $project operator****
We also have the $project operator to update a field in MongoDB. We can create a new field in the document that contains the updated value, and then use the $merge operator to replace the original document with the updated document.
In the following example, we are using the $project stage to create a new field called FullName that contains the concatenated value of $name and $father_name, separated by a space. Then, we use the $merge operator to replace the original document with the updated document. Additionally, this query updates all documents in the collection.
#Updating Documents in Collection using $project operator
db.Employees.aggregate({ $project:
{ FullName: { $concat: ["$name"," ","$father_name"] } } },
{ $merge: "Employees" })
The expected results are generated below.
To get more detail about the $project operator, you may visit MongoDB.
<a id="h-1-3-using-the-out-operator"></a>
1.3 Using the $out operator
The $out operator is used to replace an existing collection with the results of an aggregation pipeline. Now, to update a field using the value of another field in MongoDB, we can use an aggregation pipeline that includes a $project stage to create a new field based on the value of the other field and a $out stage to replace the collection with the modified documents.
Consider the implementation of the below query. The pipeline utilizes the $project operator to create a new field titled “salary” that is equal to the value of the$namefield and includes only the _id and salary fields in the output documents. Then the $out operator replaces the “Employees” collection with the modified documents.
#Modifying Documents in Collection
db.Employees.aggregate([
{ $addFields: { bonus: "$name" } },
{ $unset: "name" },
{ $out: "Employees" }
])
The output fetched from the above query.
<a id="h-1-4-using-the-addfields-operator"></a>
1.4 Using the $addFields operator
Moreover, we can use the $addFields operator with an expression that references the value of the other field to update a field using the value of another field. The $addFields operator is used to add new fields to a document. The operation adds a new field named bonus that is equal to the value of the name field below. It then removes the name field and replaces the given collection with the modified document.
#Using $addFields operator to update fields
db.Employees.aggregate([
{ $addFields: { bonus: "$name" } },
{ $unset: "name" },
{ $out: "Employees" }
])
The output yield changed the field.
<a id="h-2-conclusion"></a>
2. Conclusion
It's standard practice in MongoDB to update a column using data from another column. We have discussed the approaches to performing this task depending on our unique use case. The $set operator of MongoDB is the simplest approach while the $addFields operator enables us to add new fields and carry out additional actions on the document. When we need to substitute the modified documents for the complete collection, the $out operator comes in handy.
