liveBook · Manning

$merge

As explained in MongoDB 8.0 in Action, Third Edition

$merge is an aggregation pipeline stage that writes pipeline results to a specified collection by inserting, updating, replacing, retaining, or otherwise handling matching documents according to configured behavior.

$merge is the final stage of an aggregation pipeline when results must be written into a collection. Unlike $out, it can insert new documents, update or replace matching documents, retain existing documents, fail on matches, or apply a custom update pipeline. section 6.1.6

Use $merge when the destination requires controlled incorporation rather than wholesale replacement:

  • Write to a collection in the same or another database, including the collection being aggregated. section 6.1.6
  • Create the destination collection when it does not exist, or write into sharded collections. section 6.1.6
  • Run reads on replica set secondaries while sending writes to the primary. section 6.1.6

Merge filtered route results

1. Filter routes whose airplane is "CR2". 2. Group by _id while selecting airport and airline fields. 3. Merge into routes with on: "_id", merging matching documents and inserting unmatched documents.

Listing 6.5 The aggregation pipeline’s $merge state
db.routes.aggregate([
  {
    $match: { airplane: "CR2" }
  },
  {
    $group: {
      _id: "$_id",
      src_airport: { $first: "$src_airport" },
      dst_airport: { $first: "$dst_airport" },
      airline_name: { $first: "$airline.name" }
    }
  },
  {
    $merge: {
      into: "routes",
      on: "_id",
      whenMatched: "merge",
      whenNotMatched: "insert"
    }
  }
])

This listing shows the complete aggregation command and the essential whenMatched and whenNotMatched choices. section 6.1.6

$merge also supports operational workflows beyond ordinary aggregation. Atlas Stream Processing uses it to persist windowed results in an Atlas database, while Atlas Data Federation uses it to restore archived data into the same or another cluster, database, or collection. Archive restoration requires pausing the archive, creating a unique index on the merge fields, and ensuring sufficient destination capacity; very large partitioned datasets are discouraged. section 15.6.6 section 17.4

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains destination scope, execution placement, matching policies, and operational workflows, but does not detail every update-pipeline expression.
Examples
Demonstrates route updates, ten-second streaming windows, and archived sales restoration keyed by saleDate and customer.
What it assumes
Assumes familiarity with aggregation stages and collections; teaches destination selection, matching policies, archive pausing, and unique-index preparation.
What you can do after
Apply it via aggregation pipelines, stream processors, or Atlas Data Federation to persist results and restore archived data.
How it connects
Connects aggregation pipelines with $out, replica sets, sharded collections, Atlas Stream Processing, Atlas Data Federation, and unique indexes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.