$merge:
In MongoDB's Aggregation Framework, the $merge stage is used to merge the results of an aggregation pipeline into an existing collection or create a new collection based on the aggregated data. It is used as a stage within the aggregation pipeline rather than as an operator.
The syntax for using the $merge
stage in the aggregation pipeline is as follows:
{ $merge:
{ into: "targetCollection" or into:{db:<db name>,coll:<collection name>}
on: "matchingField",
whenMatched: "mergeAction",
whenNotMatched: "insertAction" } }
Let's break down the syntax:
$merge: This is the $merge stage in the aggregation pipeline.
{ into: "targetCollection" }: The into option specifies the target collection where the aggregated results will be merged. If the target collection does not exist, MongoDB will create it.
{ on: "matchingField" } (optional): The on option specifies the field used for matching documents in the target collection.
It indicates which field in the source documents corresponds to the field in the target collection.
If not provided, MongoDB will default to _id as the matching field.
The on specifies unique field.
{ whenMatched: "mergeAction" } (optional): The whenMatched option specifies the action to perform when a matching document is found in the target collection.
It can have one of the following values:
"mergeAction": Merges the source and target documents.
"replaceAction": Replaces the matching target document with the source document.
"keepExisting": Keeps the existing target document and ignores the source document.
"fail": Failed the aggregation operation.
{ whenNotMatched: "insertAction" } (optional): The whenNotMatched option specifies the action to perform when no matching document is found in the target collection.
It can have one of the following values:
"insertAction": Inserts the source document into the target collection.
"discard": Discards the source document and does not insert it into the target collection.
Here's an example to illustrate the $merge
stage in the aggregation pipeline:
db.sourceCollection.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: "$customerId", totalAmount: { $sum: "$amount" } } },
{ $merge: { into: "targetCollection", on: "_id", whenMatched: "mergeAction", whenNotMatched: "insertAction" } }
])
In this example, we have a source collection containing documents representing orders. We first filter the completed orders using the $match
stage.
Then, we group the orders by customerId
and calculate the total amount using the $group
stage.
Finally, we use the $merge
stage to merge the aggregated results into the targetCollection
.
We specify the matching field using on: "_id"
, and for both the whenMatched
and whenNotMatched
options, we use the default actions of "mergeAction"
and "insertAction"
, respectively.
Note that the $merge
stage is available from MongoDB version 4.2 onwards, and the specific behavior and additional options may vary depending on the version of MongoDB you are using.
0 Comments