$merge

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Pipeline stages

What it does

Writes pipeline results into a target collection.

Syntax

{$merge: { into, on?, whenMatched?, whenNotMatched? }}

Use case

Maintain materialized summaries.

Important notes

Plan idempotency and target indexes.

$merge: practical example
{ $merge: { into: "customerSummaries", on: "_id", whenMatched: "merge", whenNotMatched: "insert" } }
💡 Explanation Writes pipeline results into a target collection. When to use it Maintain materialized summaries. Caution Plan idempotency and target indexes.
Key Takeaways
  • Writes pipeline results into a target collection.
  • Typical use: Maintain materialized summaries.