Definition
Mongo.watch( pipeline, options )For replica sets and sharded clusters only
Opens a change stream cursor for a replica set or a sharded cluster to report on all its non-
systemcollections across its databases, with the exception of theadmin,local, and theconfigdatabases.Parameter Type Description pipelinearray
Optional. An Aggregation Pipeline consisting of one or more of the following aggregation stages:
Specify a pipeline to filter/modify the change events output.
Starting in MongoDB 4.2, change streams will throw an exception if the change stream aggregation pipeline modifies an event's _id field.
optionsdocument
Optional. Additional options that modify the behavior of
Mongo.watch().The
optionsdocument can contain the following fields and values:Field Type Description resumeAfterdocument
Optional. Directs
Mongo.watch()to attempt resuming notifications starting after the operation specified in the resume token.Each change stream event document includes a resume token as the
_idfield. Pass the entire_idfield of the change event document that represents the operation you want to resume after.resumeAfteris mutually exclusive withstartAfterandstartAtOperationTime.startAfterdocument
Optional. Directs
Mongo.watch()to attempt starting a new change stream after the operation specified in the resume token. Allows notifications to resume after an invalidate event.Each change stream event document includes a resume token as the
_idfield. Pass the entire_idfield of the change event document that represents the operation you want to resume after.startAfteris mutually exclusive withresumeAfterandstartAtOperationTime.fullDocumentstring
Optional. By default,
Mongo.watch()returns the delta of those fields modified by an update operation, instead of the entire updated document.Set
fullDocumentto"updateLookup"to directMongo.watch()to look up the most current majority-committed version of the updated document.Mongo.watch()returns afullDocumentfield with the document lookup in addition to theupdateDescriptiondelta.batchSizeint
Optional. Specifies the maximum number of change events to return in each batch of the response from the MongoDB cluster.
Has the same functionality as
cursor.batchSize().maxAwaitTimeMSint
Optional. The maximum amount of time in milliseconds the server waits for new data changes to report to the change stream cursor before returning an empty batch.
Defaults to
1000milliseconds.collationdocument
Optional. Pass a collation document to specify collation for the change stream cursor.
If omitted, defaults to
simplebinary comparison.startAtOperationTimeTimestamp
Optional. The starting point for the change stream. If the specified starting point is in the past, it must be in the time range of the oplog. To check the time range of the oplog, see
rs.printReplicationInfo().startAtOperationTimeis mutually exclusive withresumeAfterandstartAfter.Returns: A cursor over the change event documents. See Change Events for examples of change event documents. Tip
Compatibility
This method is available in deployments hosted in the following environments:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
- MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
- MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Availability
Deployment
Mongo.watch() is available for replica sets and sharded clusters:
- For a replica set, you can issue
Mongo.watch()on any data-bearing member. - For a sharded cluster, you must issue
Mongo.watch()on amongosinstance.
Storage Engine
You can only use Mongo.watch() with the Wired Tiger storage engine.
Read Concern majority Support
Change streams are available regardless of the "majority" read concern support; that is, read concern majority support can be either enabled (default) or disabled to use change streams.
Behavior
Mongo.watch()only notifies on data changes that have persisted to a majority of data-bearing members.The change stream cursor remains open until one of the following occurs:
- The cursor is explicitly closed.
- An invalidate event occurs; for example, a collection drop or rename.
- The connection to the MongoDB deployment closes or times out. See Behavior for more information.
- If the deployment is a sharded cluster, a shard removal may cause an open change stream cursor to close. The closed change stream cursor may not be fully resumable.
Resumability
Unlike the MongoDB Drivers, mongosh does not automatically attempt to resume a change stream cursor after an error. The MongoDB drivers make one attempt to automatically resume a change stream cursor after certain errors.
Mongo.watch() uses information stored in the oplog to produce the change event description and generate a resume token associated to that operation. If the operation identified by the resume token passed to the resumeAfter or startAfter option has already dropped off the oplog, Mongo.watch() cannot resume the change stream.
See Resume a Change Stream for more information on resuming a change stream.
Note
- You cannot use
resumeAfterto resume a change stream after an invalidate event (for example, a collection drop or rename) closes the stream. Instead, you can use startAfter to start a new change stream after an invalidate event. - If the deployment is a sharded cluster, a shard removal may cause an open change stream cursor to close. The closed change stream cursor may not be fully resumable.
Note
Resume Token
Hex Encoded Tokens
With hex-encoded string resume tokens, you can compare and sort the resume tokens. You can use either BinData resume tokens or hex string resume tokens to resume a change stream.
New resume token formats introduced in a MongoDB version cannot be consumed by earlier MongoDB versions.
Decode Resume Tokens
MongoDB provides a "snippet", an extension to mongosh, that decodes hex-encoded resume tokens.
You can install and run the resumetoken snippet from mongosh:
snippet install resumetoken
decodeResumeToken('<RESUME TOKEN>')You can also run resumetoken from the command line (without using mongosh) if npm
is installed on your system:
npx mongodb-resumetoken-decoder <RESUME TOKEN>See the following for more details on:
Full Document Lookup of Update Operations
By default, the change stream cursor returns specific field changes/deltas for update operations. You can also configure the change stream to look up and return the current majority-committed version of the changed document. Depending on other write operations that may have occurred between the update and the lookup, the returned document may differ significantly from the document at the time of the update.
Depending on the number of changes applied during the update operation and the size of the full document, there is a risk that the size of the change event document for an update operation is greater than the 16MB BSON document limit. If this occurs, the server closes the change stream cursor and returns an error.
Availability
Change streams are available regardless of the "majority" read concern support; that is, read concern majority support can be either enabled (default) or disabled to use change streams.
Access Control
When running with access control, the user must have the find and changeStream privilege actions on all non-systems collections across all databases.. That is, a user must have a role that grants the following privilege:
{ resource: { db: "", collection: "" }, actions: [ "find", "changeStream" ] }The built-in read role provides the appropriate privileges.
Cursor Iteration
MongoDB provides multiple ways to iterate on a cursor.
The cursor.hasNext() method blocks and waits for the next event. To monitor the watchCursor cursor and iterate over the events, use hasNext() like this:
while (!watchCursor.isClosed()) {
if (watchCursor.hasNext()) {
firstChange = watchCursor.next();
break;
}
}The cursor.tryNext() method is non-blocking. To monitor the watchCursor cursor and iterate over the events, use tryNext() like this:
while (!watchCursor.isClosed()) {
let next = watchCursor.tryNext()
while (next !== null) {
printjson(next);
next = watchCursor.tryNext()
}
}Example
The following operation in mongosh opens a change stream cursor on a replica set. The returned cursor reports on data changes to all the non-system collections across all databases except for the admin, local, and the config databases.
watchCursor = db.getMongo().watch()Iterate the cursor to check for new events. Use the cursor.isClosed() method with the cursor.tryNext() method to ensure the loop only exits if the change stream cursor is closed and there are no objects remaining in the latest batch:
while (!watchCursor.isClosed()) {
let next = watchCursor.tryNext()
while (next !== null) {
printjson(next);
next = watchCursor.tryNext()
}
}For complete documentation on change stream output, see Change Events.
Note
You cannot use isExhausted() with change streams.