0% completed
The aggregation pipeline in MongoDB is not only used for data analysis and transformation but also for updating documents. This advanced feature allows you to perform complex updates by utilizing the same pipeline stages and operators used in aggregation. Understanding how to leverage the aggregation pipeline for updates can greatly enhance your ability to manipulate and maintain your data.
The $merge
and $out
stages are primarily used for updating documents using the aggregation pipeline. These stages allow you to specify how the output of the pipeline should be merged with an existing collection or replace an existing collection. The $set
and $addFields
stages can also be used within the pipeline to modify the fields of documents.
Stage | Description |
---|---|
$merge | Merges the output of the aggregation pipeline into a specified collection. |
$out | Writes the resulting documents of the aggregation pipeline to a collection, replacing any existing collection with the same name. |
$set | Adds new fields or updates existing fields in the documents. |
$addFields | Adds new fields to documents. This is similar to $set but is used more for adding new fields rather than updating existing ones. |
First, let's insert some documents into the orders
collection to work with:
db.orders.insertMany([ { _id: 1, item: "apple", quantity: 5, price: 10 }, { _id: 2, item: "banana", quantity: 10, price: 20 }, { _id: 3, item: "orange", quantity: 15, price: 15 }, { _id: 4, item: "mango", quantity: 20, price: 25 } ])
The $set
stage is used to add new fields or update existing fields in the documents. This stage can be used within an aggregation pipeline to perform updates.
Update the Price of Each Item
db.orders.aggregate([ { $set: { price: { $multiply: ["$price", 1.1] } } } ])
This command increases the price
of each item by 10%. The $set
stage updates the price
field by multiplying its current value by 1.1. For example, if an item's current price is 10, the updated price will be 11.
Add a New Field "totalPrice"
db.orders.aggregate([ { $set: { totalPrice: { $multiply: ["$price", "$quantity"] } } } ])
This command adds a new field totalPrice
to each document, which is calculated by multiplying the price
and quantity
fields. For example, if an item's price is 10 and its quantity is 5, the totalPrice
will be 50.
The $merge
stage merges the output of the aggregation pipeline into a specified collection. It can either insert the documents into the collection, update the existing documents, or replace the documents.
Merge Updated Documents into the Same Collection
db.orders.aggregate([ { $set: { totalPrice: { $multiply: ["$price", "$quantity"] } } }, { $merge: { into: "orders", on: "_id", whenMatched: "merge", whenNotMatched: "insert" } } ])
This command calculates the totalPrice
for each document and merges the updated documents back into the orders
collection. If a document with the same _id
exists, it will be updated; if not, a new document will be inserted.
Merge Documents into a New Collection
db.orders.aggregate([ { $set: { totalPrice: { $multiply: ["$price", "$quantity"] } } }, { $merge: { into: "orders_summary", on: "_id", whenMatched: "merge", whenNotMatched: "insert" } } ])
This command calculates the totalPrice
for each document and merges the results into a new collection called orders_summary
. This is useful for creating summary collections based on aggregations.
The $out
stage writes the resulting documents of the aggregation pipeline to a collection, replacing any existing collection with the same name.
Write Aggregated Results to a New Collection
db.orders.aggregate([ { $set: { totalPrice: { $multiply: ["$price", "$quantity"] } } }, { $out: "orders_summary" } ])
This command calculates the totalPrice
for each document and writes the results to a new collection called orders_summary
. If the orders_summary
collection already exists, it will be replaced.
Using the aggregation pipeline for updates in MongoDB provides a powerful way to perform complex updates and transformations on your data. The $set
, $merge
, and $out
stages are key to leveraging the full potential of the aggregation framework for updating documents.
.....
.....
.....