change_stream – Watch changes on a collection, database, or cluster

Watch changes on a collection, a database, or the entire cluster.

class pymongo.change_stream.ChangeStream(target, pipeline, full_document, resume_after, max_await_time_ms, batch_size, collation, start_at_operation_time, session)

The internal abstract base class for change stream cursors.

Should not be called directly by application developers. Use pymongo.collection.Collection.watch(), pymongo.database.Database.watch(), or pymongo.mongo_client.MongoClient.watch() instead.

New in version 3.6.

See also

The MongoDB documentation on

changeStreams

alive

Does this cursor have the potential to return more data?

Note

Even if alive is True, next() can raise StopIteration and try_next() can return None.

New in version 3.8.

close()

Close this ChangeStream.

next()

Advance the cursor.

This method blocks until the next change document is returned or an unrecoverable error is raised. This method is used when iterating over all changes in the cursor. For example:

try:
    with db.collection.watch(
            [{'$match': {'operationType': 'insert'}}]) as stream:
        for insert_change in stream:
            print(insert_change)
except pymongo.errors.PyMongoError:
    # The ChangeStream encountered an unrecoverable error or the
    # resume attempt failed to recreate the cursor.
    logging.error('...')

Raises StopIteration if this ChangeStream is closed.

try_next()

Advance the cursor without blocking indefinitely.

This method returns the next change document without waiting indefinitely for the next change. For example:

with db.collection.watch() as stream:
    while stream.alive:
        change = stream.try_next()
        if change is not None:
            print(change)
        elif stream.alive:
            # We end up here when there are no recent changes.
            # Sleep for a while to avoid flooding the server with
            # getMore requests when no changes are available.
            time.sleep(10)

If no change document is cached locally then this method runs a single getMore command. If the getMore yields any documents, the next document is returned, otherwise, if the getMore returns no documents (because there have been no changes) then None is returned.

Returns:The next change document or None when no document is available after running a single getMore or when the cursor is closed.

New in version 3.8.

class pymongo.change_stream.ClusterChangeStream(target, pipeline, full_document, resume_after, max_await_time_ms, batch_size, collation, start_at_operation_time, session)

A change stream that watches changes on all collections in the cluster.

Should not be called directly by application developers. Use helper method pymongo.mongo_client.MongoClient.watch() instead.

New in version 3.7.

class pymongo.change_stream.CollectionChangeStream(target, pipeline, full_document, resume_after, max_await_time_ms, batch_size, collation, start_at_operation_time, session)

A change stream that watches changes on a single collection.

Should not be called directly by application developers. Use helper method pymongo.collection.Collection.watch() instead.

New in version 3.7.

class pymongo.change_stream.DatabaseChangeStream(target, pipeline, full_document, resume_after, max_await_time_ms, batch_size, collation, start_at_operation_time, session)

A change stream that watches changes on all collections in a database.

Should not be called directly by application developers. Use helper method pymongo.database.Database.watch() instead.

New in version 3.7.