Filter events
Receive only the events you care about with a pipeline in watch.
Receive only the events you care about by passing a pipeline to watch. The filter runs on the server, so the other events do not travel over the network.
Requirements
- A user with the
findandchangeStreamprivileges on what you watch.
Filter by type and by field
JavaScript
const stream = db.orders.watch([
{ $match: { operationType: "update", "updateDescription.updatedFields.status": "paid" } },
{ $project: { documentKey: 1, "updateDescription.updatedFields": 1 } },
]);Bring the whole document
JavaScript
const stream = db.orders.watch([], { fullDocument: "updateLookup" });Check
With the stream open, an updateOne that changes the status to paid produces an event and one that changes another field does not.
Common problems
- A
$projectthat removes_idleaves the event without a resume token and the stream fails.