Change Streams
On this page
New in version 3.6.
Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog. Applications can use change streams to subscribe to all data changes on a collection and immediately react to them.
Important
Change stream is only available if "majority"
read concern support is enabled (default).
Open A Change Stream
You can only open a change stream against replica sets or sharded clusters. For a sharded cluster, you must issue the open change stream operation against the mongos
.
The replica set or the sharded cluster must use replica set protocol version 1 (pv1
) and WiredTiger storage engine (can be encrypted).
The following example opens a change stream for a collection and iterates over the cursor to retrieve the change stream documents. While the connection to the MongoDB deployment remains open, the cursor remains open until one of the following occurs:
- The cursor is explicitly closed.
- An invalidate event occurs.
- If the deployment is a sharded cluster, a shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.
- Python
- Java (Sync)
- Node.js
- PHP
- Motor
- Other
The Python examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The Java examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The Node.js examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The following example uses stream to process the change events.
Alternatively, you can also use iterator to process the change events:
The examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The C examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The C# examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
The examples below assume that you have connected to a MongoDB replica set and have accessed a database that contains an inventory
collection.
To retrieve the data change event notifications, iterate the change stream cursor
.
Note
The lifecycle of an unclosed cursor is language-dependent.
See Change Events for more information on the change stream response document format.
Modify Change Stream Output
- Python
- Java (Sync)
- Node.js
- PHP
- Motor
- Other
You can control change stream output by providing an array of one or more of the following pipeline stages when configuring the change stream:
You can control change stream output by providing an array of one or more of the following pipeline stages when configuring the change stream:
The pipeline
list includes a single $match
stage that filters any operations where the username
is alice
, or operations where the operationType
is delete
.
Passing the pipeline
to the watch()
method directs the change stream to return notifications after passing them through the specified pipeline
.
You can control change stream output by providi