MongoDB Transactions and Change Streams/Watch only selected changes

Filter stream events with a pipeline.

Section: Change streams

Watch only selected changes

javascript
javascript
const stream = db.orders.watch([{ $match: { operationType: "insert" } }])
Explanation

Filtering early helps keep change events focused and lighter to process.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Change streams
Watch a collection
Open a change stream on one collection.
OpenIn sheetjavascriptsame section
Read the next change event
Consume the stream in mongosh.
OpenIn sheetjavascriptsame section
Start a session
Create a session for transactional work.
OpenIn sheetjavascript1 tag match
Start a transaction
Begin a multi-document transaction.
OpenIn sheetjavascript1 tag match
Transaction example
Insert and update as one transaction.
OpenIn sheetjavascript1 tag match
Abort a transaction
Roll back a started transaction.
OpenIn sheetjavascript1 tag match