- Reference >
- MongoDB\Collection Class >
- MongoDB\Collection::watch()
MongoDB\Collection::watch()
New in version 1.3.
On this page
Definition
-
MongoDB\Collection::watch Executes a change stream operation on the collection. The change stream can be watched for collection-level changes.
This method has the following parameters:
Parameter Type Description $pipelinearray|object Optional. The pipeline of stages to append to an initial $changeStreamstage.$optionsarray Optional. An array specifying the desired options. The
$optionsparameter supports the following options:Option Type Description batchSizeinteger Optional. Specifies the maximum number of change events to return in each batch of the response from the MongoDB cluster. collationarray|object Optional. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the
localefield is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.This option is available in MongoDB 3.4+ and will result in an exception at execution time if specified for an older server version.
fullDocumentstring Optional. Allowed values are ‘default’ and ‘updateLookup’. Defaults to ‘default’. When set to ‘updateLookup’, the change notification for partial updates will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred. The following values are supported:
MongoDB\Operation\Watch::FULL_DOCUMENT_DEFAULT(default)MongoDB\Operation\Watch::FULL_DOCUMENT_UPDATE_LOOKUP
Note
This is an option of the
$changeStreampipeline stage.maxAwaitTimeMSinteger Optional. Positive integer denoting the time limit in milliseconds for the server to block a getMore operation if no data is available. readConcernMongoDB\Driver\ReadConcern Optional. Read concern to use for the operation. Defaults to the collection’s read concern.
This is not supported for server versions prior to 3.2 and will result in an exception at execution time if used.
It is not possible to specify a read concern for individual operations as part of a transaction. Instead, set the
readConcernoption when starting the transaction with startTransaction.readPreferenceMongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the collection’s read preference.
This is used for both the initial change stream aggregation and for server selection during an automatic resume.
resumeAfterarray|object Optional. Specifies the logical starting point for the new change stream. The
_idfield in documents returned by the change stream may be used here.Using this option in conjunction with
startAfterand/orstartAtOperationTimewill result in a server error. The options are mutually exclusive.Note
This is an option of the
$changeStreampipeline stage.sessionMongoDB\Driver\Session Optional. Client session to associate with the operation.
Sessions are not supported for server versions prior to 3.6.
startAfterarray|object Optional. Specifies the logical starting point for the new change stream. The
_idfield in documents returned by the change stream may be used here. UnlikeresumeAfter, this option can be used with a resume token from an “invalidate” event.Using this option in conjunction with
resumeAfterand/orstartAtOperationTimewill result in a server error. The options are mutually exclusive.This is not supported for server versions prior to 4.2 and will result in an exception at execution time if used.
Note
This is an option of the
$changeStreampipeline stage.startAtOperationTimeMongoDB\BSON\TimestampInterface Optional. If specified, the change stream will only provide changes that occurred at or after the specified timestamp. Command responses from a MongoDB 4.0+ server include an
operationTimethat can be used here. By default, theoperationTimereturned by the initialaggregatecommand will be used if available.Using this option in conjunction with
resumeAfterand/orstartAfterwill result in a server error. The options are mutually exclusive.This is not supported for server versions prior to 4.0 and will result in an exception at execution time if used.
Note
This is an option of the
$changeStreampipeline stage.New in version 1.4.
typeMaparray Optional. The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the collection’s type map.
Return Values
A MongoDB\ChangeStream object, which allows for iteration of
events in the change stream via the Iterator interface.
Errors/Exceptions
MongoDB\Exception\UnexpectedValueException if the command
response from the server was malformed.
MongoDB\Exception\UnsupportedException if options are used and
not supported by the selected server (e.g. collation, readConcern,
writeConcern).
MongoDB\Exception\InvalidArgumentException for errors related to
the parsing of parameters or options.
MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).
Examples
This example reports events while iterating a change stream.
Assuming that a document was inserted, updated, and deleted while the above script was iterating the change stream, the output would then resemble:
See Also
MongoDB\Client::watch()MongoDB\Database::watch()- Aggregation Pipeline documentation in the MongoDB Manual
- Change Streams documentation in the MongoDB manual
- Change Events documentation in the MongoDB manual