The first argument to watch() is an aggregation pipeline applied to change events server-side before being returned to the client. Only $match, $project, $addFields, $replaceRoot, $replaceWith, $redact, and $set/$unset are allowed.
const stream = db.collection("orders").watch([
{ $match: { operationType: { $in: ["insert", "update"] } } }
])
const stream = db.collection("orders").watch([
{
$match: {
operationType: "update",
"updateDescription.updatedFields.status": { $exists: true }
}
}
])
const stream = db.collection("orders").watch(
[
{ $match: {
operationType: { $in: ["insert", "update"] },
"fullDocument.total": { $gte: 1000 }
}}
],
{ fullDocument: "updateLookup" }
)
const stream = db.collection("users").watch([
{ $match: { operationType: { $in: ["insert", "update"] } } },
{ $project: {
_id: 1,
operationType: 1,
"documentKey._id": 1,
"fullDocument.email": 1,
"fullDocument.status": 1,
"fullDocument.passwordHash": 0
}}
])
const dbStream = db.watch([
{ $match: { "ns.coll": { $in: ["orders", "payments"] } } }
])
NOTE
Filtering with a $match stage happens on the server — events that don't match are never sent to the client. This reduces network traffic and client processing cost, especially on high-write collections. Prefer server-side filtering over client-side filtering whenever possible.