watch()

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

Reference group: Indexes and aggregation

What it does

Opens a change stream on supported deployments.

Syntax

db.collection.watch(pipeline, options)

Use case

React to data changes in event-driven systems.

Important notes

Handle resume tokens and reconnections.

watch(): practical example
db.orders.watch([{ $match: { operationType: "insert" } }])
💡 Explanation Opens a change stream on supported deployments. When to use it React to data changes in event-driven systems. Caution Handle resume tokens and reconnections.
Key Takeaways
  • Opens a change stream on supported deployments.
  • Typical use: React to data changes in event-driven systems.