Open In App

How to Update MongoDB Field Using Value of Another Field

Last Updated : 09 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In the NoSQL database, MongoDB is the most robust and flexible option for managing big data. One common problem that occurs when working with MongoDB is to Update the MongoDB field using the value of another field and change data within our database.

In this article, we will learn about How to Update the MongoDB field using the value of another field by understanding various methods along with the practical implementation and so on.

How to Update the MongoDB Field Using the Value of Another Field?

When working with MongoDB, we need to change or update the data in our database. This process is known as data manipulation. MongoDB provides several ways to manipulate data, including updating, deleting, and transforming it. Below is the method that helps us to Update the MongoDB field using the value of another field as follows:

  1. Using the $set Operator
  2. Using Aggregation Pipeline with $addFields
  3. Using Aggregation Pipeline with $set

Let’s set up an environment

db.products.insertMany([
{
"_id": 1,
"name": "Laptop",
"brand": "Dell",
"price": 1200,
"discount": 100
},
{
"_id": 2,
"name": "Smartphone",
"brand": "Samsung",
"price": 800,
"discount": 50
},
{
"_id": 3,
"name": "Headphones",
"brand": "Sony",
"price": 150,
"discount": 10
},
{
"_id": 4,
"name": "Tablet",
"brand": "Apple",
"price": 900,
"discount": 75
}
]);

Output:

productCollection1

1. Using the $set Operator

This method directly modifies the value of a field belong to a document in a MongoDB collection.

// Putting 'discounted_price' field up by using $set operator as 'price' and 'discount'
db.products.updateMany(
{},
{ $set: { "discounted_price": { $subtract: ["$price", "$discount"] } }
);

// retrieves the result
db.products.find();

Output:

method1

Explanation:  In the given query we update the discounted_price field in the products collection discounting from the price field with the $set operator, and then retrieves all products from from the products collection.

2. Using Aggregation Pipeline with $addFields

This method can add new fields to a document or make changes to existing fields by using the aggregation framework’s $addField stage. It makes it simple to do multiple operations for complex operations.

// Update 'full_name' field by concatenating 'first_name' and 'last_name' within aggregation pipeline
db.products.aggregate([
{
$addFields: {
"full_name": {"$concat": ["$name", " ", "$brand"]}
}
},
{
$out: "products" // Insert up-to-date records to replace old documents
}
]);
// retrieves the result
db.products.find();

Output:

METHOD2

Explanation: The above query updates the full_name field in the products collection using an aggregation pipeline by concatenating the name and brand fields and overwrites the original collection with the updated documents after that retrieves all documents from the products collection.

3. Using Aggregation Pipeline with $set

This method updates fields in a document using the aggregation framework’s $set stage. It allows for more advanced updates and can handle conditional updates or updates based on other fields in the document.

// Updating 'Discounted price' field as 'Price' field minus 'Discounted price' field using aggregation pipeline
db.products.aggregate([
{
$set: {
"discounted_price": { $subtract: ["$price", "$discount"] }
}
},
{
$out: "products" // remove the previously existing documents and replace them with new documents
}
]);
// retrieves the result
db.products.find();

Output:

Using-Aggregation-Pipeline-with-$set

Explanation: In the above query we discounted_price field in the products collection by subtracting the discount from the price using an aggregation pipeline and overwrites the existing collection with the updated documents. then retrieves all documents from the products collection.

Conclusion

Overall, Updating fields in MongoDB using values from other fields is a common requirement in database management. MongoDB offers a variety of methods including the $set operator, aggregation pipelines to solve this efficiently. By understanding these methods and best practices developers can maintain data integrity and perform complex data manipulations easily.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads