$mergeObjects as object expression operator

APPLIES TO: MongoDB vCore

The $mergeObjects operator is used to combine multiple documents into a single document. This operator is useful in aggregation pipelines when you need to merge fields from different documents or add another field to an existing document. The behavior of $mergeObjects is to overwrite fields in the target document with fields from the source document when there are conflicts.

Syntax

{ $mergeObjects: [ <document1>, <document2>, ... ] }

Parameters

Description
document1, document2 These documents are targeted for merge. The documents can be specified as field paths, subdocuments, or constants.