Watch changes on this database. Performs an aggregation with an implicit initial ``$changeStream`` stage and returns a :class:`~pymongo.change_stream.DatabaseChangeStream` cursor which iterates over changes on all collections in this database. Introduced in
(
self,
pipeline: Optional[_Pipeline] = None,
full_document: Optional[str] = None,
resume_after: Optional[Mapping[str, Any]] = None,
max_await_time_ms: Optional[int] = None,
batch_size: Optional[int] = None,
collation: Optional[_CollationIn] = None,
start_at_operation_time: Optional[Timestamp] = None,
session: Optional[ClientSession] = None,
start_after: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
full_document_before_change: Optional[str] = None,
show_expanded_events: Optional[bool] = None,
)
| 346 | ) |
| 347 | |
| 348 | def watch( |
| 349 | self, |
| 350 | pipeline: Optional[_Pipeline] = None, |
| 351 | full_document: Optional[str] = None, |
| 352 | resume_after: Optional[Mapping[str, Any]] = None, |
| 353 | max_await_time_ms: Optional[int] = None, |
| 354 | batch_size: Optional[int] = None, |
| 355 | collation: Optional[_CollationIn] = None, |
| 356 | start_at_operation_time: Optional[Timestamp] = None, |
| 357 | session: Optional[ClientSession] = None, |
| 358 | start_after: Optional[Mapping[str, Any]] = None, |
| 359 | comment: Optional[Any] = None, |
| 360 | full_document_before_change: Optional[str] = None, |
| 361 | show_expanded_events: Optional[bool] = None, |
| 362 | ) -> DatabaseChangeStream[_DocumentType]: |
| 363 | """Watch changes on this database. |
| 364 | |
| 365 | Performs an aggregation with an implicit initial ``$changeStream`` |
| 366 | stage and returns a |
| 367 | :class:`~pymongo.change_stream.DatabaseChangeStream` cursor which |
| 368 | iterates over changes on all collections in this database. |
| 369 | |
| 370 | Introduced in MongoDB 4.0. |
| 371 | |
| 372 | .. code-block:: python |
| 373 | |
| 374 | with db.watch() as stream: |
| 375 | for change in stream: |
| 376 | print(change) |
| 377 | |
| 378 | The :class:`~pymongo.change_stream.DatabaseChangeStream` iterable |
| 379 | blocks until the next change document is returned or an error is |
| 380 | raised. If the |
| 381 | :meth:`~pymongo.change_stream.DatabaseChangeStream.next` method |
| 382 | encounters a network error when retrieving a batch from the server, |
| 383 | it will automatically attempt to recreate the cursor such that no |
| 384 | change events are missed. Any error encountered during the resume |
| 385 | attempt indicates there may be an outage and will be raised. |
| 386 | |
| 387 | .. code-block:: python |
| 388 | |
| 389 | try: |
| 390 | with db.watch([{"$match": {"operationType": "insert"}}]) as stream: |
| 391 | for insert_change in stream: |
| 392 | print(insert_change) |
| 393 | except pymongo.errors.PyMongoError: |
| 394 | # The ChangeStream encountered an unrecoverable error or the |
| 395 | # resume attempt failed to recreate the cursor. |
| 396 | logging.error("...") |
| 397 | |
| 398 | For a precise description of the resume process see the |
| 399 | `change streams specification`_. |
| 400 | |
| 401 | :param pipeline: A list of aggregation pipeline stages to |
| 402 | append to an initial ``$changeStream`` stage. Not all |
| 403 | pipeline stages are valid after a ``$changeStream`` stage, see the |
| 404 | MongoDB documentation on change streams for the supported stages. |
| 405 | :param full_document: The fullDocument to pass as an option |