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

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

    The internal abstract base class for change stream cursors.

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

    New in version 3.6.

    See also

    The MongoDB documentation on .

    • alive

      Does this cursor have the potential to return more data?

      Note

      Even if alive is True, can raise StopIteration and can return None.

      New in version 3.8.

    • 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:

      Raises StopIteration if this ChangeStream is closed.

    • resume_token

      The cached resume token that will be used to resume after the most recently returned change.

      New in version 3.9.

    • ()

      Advance the cursor without blocking indefinitely.

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

      1. while stream.alive:
      2. change = stream.try_next()
      3. # Note that the ChangeStream's resume token may be updated
      4. # even when no changes are returned.
      5. print("Current resume token: %r" % (stream.resume_token,))
      6. print("Change document: %r" % (change,))
      7. # We end up here when there are no recent changes.
      8. # Sleep for a while before trying again to avoid flooding
      9. # the server with getMore requests when no changes are
      10. time.sleep(10)

      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, start_after)

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

    Should not be called directly by application developers. Use helper method instead.

    New in version 3.7.

    class (target, pipeline, full_document, resume_after, max_await_time_ms, batch_size, collation, start_at_operation_time, session, start_after)

    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, start_after)

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

    New in version 3.7.