Docs Home → MongoDB Manual
Note
This page describes the $merge
stage, which outputs the
aggregation pipeline results to a collection. For the $mergeObjects
operator, which merges documents into a single document, see $mergeObjects
.
$merge
New in version 4.2.
Writes the results of the aggregation pipeline to a specified collection. The
$merge
operator must be the last stage in the pipeline.
The $merge
stage:
Can output to a collection in the same or different database.
Starting in MongoDB 4.4:
$merge
can output to the same collection that is being aggregated. For more information, see Output to the Same Collection that is Being Aggregated.Pipelines with the
$merge
stage can run on replica set secondary nodes if all the nodes in cluster have featureCompatibilityVersion set to4.4
or higher and the Read Preference allows secondary reads.Read operations of the
$merge
statement are sent to secondary nodes, while the write operations occur only on the primary node.Not all driver versions support targeting of
$merge
operations to replica set secondary nodes. Check your driver documentation to see when your driver added support for$merge
read operations running on secondary nodes.
Creates a new collection if the output collection does not already exist.
Can incorporate results [insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline] into an existing collection.
Can output to a sharded collection. Input collection can also be sharded.
For a comparison with the $out
stage which also outputs the aggregation results to a collection, see
$merge
and $out
Comparison.
Note
On-Demand Materialized Views
$merge
can incorporate the pipeline results into an existing
output collection rather than perform a full replacement of the collection. This functionality allows users to create on-demand materialized views, where the content of the output collection is incrementally updated when the pipeline is run.
For more information on this use case, see On-Demand Materialized Views as well as the examples on this page.
Materialized views are separate from read-only views. For information on creating read-only views, see read-only views.
$merge
has the following syntax:
{ $merge: { into: -or- { db: , coll: }, on: -or- [ , ...], // Optional let: , // Optional whenMatched: , // Optional whenNotMatched: // Optional } }
For example:
{ $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }
If using all default options for
$merge
, including writing to a collection in the same database, you can use the simplified form:
{ $merge: } // Output collection is in the same database
The $merge
takes a document with the following fields:
into | The output collection. Specify either:
Note
|
on
Optional. Field or fields that act as a unique identifier for a document. The identifier determines if a results document matches an existing document in the output collection. Specify either:
A single field name as a string. For example:
on: "_id"
A combination of fields in an array. For example:
on: [ "date", "customerId" ]
The order of the fields in the array does not matter, and you cannot specify the same field multiple times.
For the specified field or fields:
The aggregation results documents must contain the field[s] specified in the
on
, unless theon
field is the_id
field. If the_id
field is missing from a results document, MongoDB adds it automatically.The specified field or fields cannot contain a null or an array value.
$merge
requires a unique, index with keys that correspond to the
on identifier fields. Although the order of the index key specification does not matter, the unique index must only contain the on
fields as its keys.
The index must also have the same collation as the aggregation's collation.
The unique index can be a sparse index.
The unique index cannot be a partial index.
For output collections that already exist, the corresponding index must already exist.
The default value for on depends on the output collection:
If the output collection does not exist, the on identifier must be and defaults to the
_id
field. The corresponding unique_id
index is automatically created.Tip
To use a different on identifier field[s] for a collection that does not exist, you can create the collection first by creating a unique index on the desired field[s]. See the section on non-existent output collection for an example.
If the existing output collection is unsharded, the on
identifier defaults to the _id
field.
If the existing output collection is a sharded collection, the on identifier defaults to all the shard key fields and the _id
field. If specifying a different on
identifier, the on
must
contain all the shard key fields.
whenMatched
Optional. The behavior of $merge
if a result document and an existing document in the collection have the same value for the specified on field[s].
You can specify either:
One of the pre-defined action strings:
Action
Description
"replace"
Replace the existing document in the output collection with the matching results document.
When performing a replace, the replacement document cannot result in a modification of the
_id
value or, if the output collection is sharded, the shard key value. Otherwise, the operation generates an error.Tip
To avoid this error, if the on field does not include the
_id
field, remove the_id
field in the aggregation results to avoid the error, such as with a preceding$unset
stage, and so on.
"keepExisting"
Keep the existing document in the output collection.
"merge" [Default]
Merge the matching documents [similar to the $mergeObjects
operator].
If the results document contains fields not in the existing document, add these new fields to the existing document.
If the results document contains fields in the existing document, replace the existing field values with those from the results document.
For example, if the output collection has the document:
{ _id: 1, a: 1, b: 1 }
And the aggregation results has the document:
{ _id: 1, b: 5, z: 1 }
Then, the merged document is:
{ _id: 1, a: 1, b: 5, z: 1 }
When performing a merge, the merged document cannot result in a modification of the _id
value or, if the output collection is sharded, the shard key value. Otherwise, the operation generates an error.
Tip
To
avoid this error, if the on field does not include the _id
field, remove the _id
field in the aggregation results to avoid the error, such as with a preceding $unset
stage, and so on.