$unionWith

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

Reference group: Pipeline stages

What it does

Combines pipeline results from another collection.

Syntax

{$unionWith: { coll, pipeline? }}

Use case

Query current and archived records together.

$unionWith: practical example
{ $unionWith: { coll: "archivedOrders", pipeline: [{ $match: { status: "paid" } }] } }
💡 Explanation Combines pipeline results from another collection. When to use it Query current and archived records together.
Key Takeaways
  • Combines pipeline results from another collection.
  • Typical use: Query current and archived records together.