Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
eb2cd59f
Commit
eb2cd59f
authored
Jun 26, 2018
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-351: Documentation for watch() methods
parent
9ccfae84
Show whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
561 additions
and
93 deletions
+561
-93
apiargs-MongoDBClient-common-option.yaml
docs/includes/apiargs-MongoDBClient-common-option.yaml
+37
-0
apiargs-MongoDBClient-method-dropDatabase-option.yaml
...des/apiargs-MongoDBClient-method-dropDatabase-option.yaml
+4
-10
apiargs-MongoDBClient-method-watch-option.yaml
docs/includes/apiargs-MongoDBClient-method-watch-option.yaml
+44
-0
apiargs-MongoDBClient-method-watch-param.yaml
docs/includes/apiargs-MongoDBClient-method-watch-param.yaml
+8
-0
apiargs-MongoDBCollection-common-option.yaml
docs/includes/apiargs-MongoDBCollection-common-option.yaml
+4
-17
apiargs-MongoDBCollection-method-watch-option.yaml
...cludes/apiargs-MongoDBCollection-method-watch-option.yaml
+19
-50
apiargs-MongoDBCollection-method-watch-param.yaml
...ncludes/apiargs-MongoDBCollection-method-watch-param.yaml
+4
-9
apiargs-MongoDBDatabase-common-option.yaml
docs/includes/apiargs-MongoDBDatabase-common-option.yaml
+23
-0
apiargs-MongoDBDatabase-method-createCollection-option.yaml
...iargs-MongoDBDatabase-method-createCollection-option.yaml
+1
-1
apiargs-MongoDBDatabase-method-watch-option.yaml
...includes/apiargs-MongoDBDatabase-method-watch-option.yaml
+44
-0
apiargs-MongoDBDatabase-method-watch-param.yaml
.../includes/apiargs-MongoDBDatabase-method-watch-param.yaml
+8
-0
apiargs-common-option.yaml
docs/includes/apiargs-common-option.yaml
+17
-0
apiargs-method-watch-option.yaml
docs/includes/apiargs-method-watch-option.yaml
+81
-0
apiargs-method-watch-param.yaml
docs/includes/apiargs-method-watch-param.yaml
+13
-0
MongoDBClient.txt
docs/reference/class/MongoDBClient.txt
+1
-0
MongoDBDatabase.txt
docs/reference/class/MongoDBDatabase.txt
+1
-0
MongoDBClient-watch.txt
docs/reference/method/MongoDBClient-watch.txt
+121
-0
MongoDBCollection-watch.txt
docs/reference/method/MongoDBCollection-watch.txt
+11
-6
MongoDBDatabase-watch.txt
docs/reference/method/MongoDBDatabase-watch.txt
+120
-0
No files found.
docs/includes/apiargs-MongoDBClient-common-option.yaml
0 → 100644
View file @
eb2cd59f
arg_name
:
option
name
:
readConcern
type
:
:php:`MongoDB\\Driver\\ReadConcern <class.mongodb-driver-readconcern>`
description
:
|
:manual:`Read concern </reference/read-concern>` to use for the operation.
Defaults to the client'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.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
readPreference
type
:
:php:`MongoDB\\Driver\\ReadPreference <class.mongodb-driver-readpreference>`
description
:
|
:manual:`Read preference </reference/read-preference>` to use for the
operation. Defaults to the client's read preference.
interface
:
phpmethod
operation
:
~
optional
:
true
---
source
:
file
:
apiargs-common-option.yaml
ref
:
typeMap
---
arg_name
:
option
name
:
writeConcern
type
:
:php:`MongoDB\\Driver\\WriteConcern <class.mongodb-driver-writeconcern>`
description
:
|
:manual:`Write concern </reference/write-concern>` to use for the operation.
Defaults to the client's write concern.
interface
:
phpmethod
operation
:
~
optional
:
true
...
docs/includes/apiargs-MongoDBClient-method-dropDatabase-option.yaml
View file @
eb2cd59f
...
@@ -10,16 +10,10 @@ source:
...
@@ -10,16 +10,10 @@ source:
post
:
|
post
:
|
This will be used for the returned command result document.
This will be used for the returned command result document.
---
---
arg_name
:
option
source
:
name
:
writeConcern
file
:
apiargs-MongoDBClient-common-option.yaml
type
:
:php:`MongoDB\\Driver\\WriteConcern <class.mongodb-driver-writeconcern>`
ref
:
writeConcern
description
:
|
post
:
|
:manual:`Write concern </reference/write-concern>` to use for the operation.
Defaults to the client's write concern.
This is not supported for server versions prior to 3.4 and will result in an
This is not supported for server versions prior to 3.4 and will result in an
exception at execution time if used.
exception at execution time if used.
interface
:
phpmethod
operation
:
~
optional
:
true
...
...
docs/includes/apiargs-MongoDBClient-method-watch-option.yaml
0 → 100644
View file @
eb2cd59f
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
batchSize
---
source
:
file
:
apiargs-common-option.yaml
ref
:
collation
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
fullDocument
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
maxAwaitTimeMS
---
source
:
file
:
apiargs-MongoDBClient-common-option.yaml
ref
:
readConcern
---
source
:
file
:
apiargs-MongoDBClient-common-option.yaml
ref
:
readPreference
post
:
|
This is used for both the initial change stream aggregation and for
server selection during an automatic resume.
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
resumeAfter
---
source
:
file
:
apiargs-common-option.yaml
ref
:
session
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
startAtOperationTime
---
source
:
file
:
apiargs-MongoDBClient-common-option.yaml
ref
:
typeMap
...
docs/includes/apiargs-MongoDBClient-method-watch-param.yaml
0 → 100644
View file @
eb2cd59f
source
:
file
:
apiargs-method-watch-param.yaml
ref
:
$pipeline
---
source
:
file
:
apiargs-method-watch-param.yaml
ref
:
$options
...
docs/includes/apiargs-MongoDBCollection-common-option.yaml
View file @
eb2cd59f
...
@@ -24,27 +24,14 @@ interface: phpmethod
...
@@ -24,27 +24,14 @@ interface: phpmethod
operation
:
~
operation
:
~
optional
:
true
optional
:
true
---
---
arg_name
:
option
source
:
name
:
collation
file
:
apiargs-common-option.yaml
type
:
array|object
ref
:
collation
description
:
|
post
:
|
:manual:`Collation </reference/collation>` allows users to specify
language-specific rules for string comparison, such as rules for lettercase
and accent marks. When specifying collation, the ``locale`` field is
mandatory; all other collation fields are optional. For descriptions of the
fields, see :manual:`Collation Document
</reference/collation/#collation-document>`.
If the collation is unspecified but the collection has a default collation,
If the collation is unspecified but the collection has a default collation,
the operation uses the collation specified for the collection. If no
the operation uses the collation specified for the collection. If no
collation is specified for the collection or for the operation, MongoDB uses
collation is specified for the collection or for the operation, MongoDB uses
the simple binary comparison used in prior versions for string comparisons.
the simple binary comparison used in prior versions for string comparisons.
This option is available in MongoDB 3.4+ and will result in an exception at
execution time if specified for an older server version.
interface
:
phpmethod
operation
:
~
optional
:
true
---
---
arg_name
:
option
arg_name
:
option
name
:
readConcern
name
:
readConcern
...
...
docs/includes/apiargs-MongoDBCollection-method-watch-option.yaml
View file @
eb2cd59f
---
---
arg_name
:
option
source
:
name
:
batchSize
file
:
apiargs-method-watch-option.yaml
type
:
integer
ref
:
batchSize
description
:
|
Specifies the maximum number of change events to return in each batch of the
response from the MongoDB cluster.
interface
:
phpmethod
operation
:
~
optional
:
true
---
---
source
:
source
:
file
:
apiargs-
MongoDBCollection-
common-option.yaml
file
:
apiargs-common-option.yaml
ref
:
collation
ref
:
collation
---
---
arg_name
:
option
source
:
name
:
fullDocument
file
:
apiargs-method-watch-option.yaml
type
:
string
ref
:
fullDocument
description
:
|
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 ``$changeStream`` pipeline stage.
interface
:
phpmethod
operation
:
~
optional
:
true
---
---
arg_name
:
option
source
:
name
:
maxAwaitTimeMS
file
:
apiargs-method-watch-option.yaml
type
:
integer
ref
:
maxAwaitTimeMS
description
:
|
Positive integer denoting the time limit in milliseconds for the server to
block a getMore operation if no data is available.
interface
:
phpmethod
operation
:
~
optional
:
true
---
---
source
:
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
file
:
apiargs-MongoDBCollection-common-option.yaml
...
@@ -54,23 +26,20 @@ post: |
...
@@ -54,23 +26,20 @@ post: |
This is used for both the initial change stream aggregation and for
This is used for both the initial change stream aggregation and for
server selection during an automatic resume.
server selection during an automatic resume.
---
---
arg_name
:
option
source
:
name
:
resumeAfter
file
:
apiargs-method-watch-option.yaml
type
:
array|object
ref
:
resumeAfter
description
:
|
Specifies the logical starting point for the new change stream.
.. note::
This is an option of the ``$changeStream`` pipeline stage.
interface
:
phpmethod
operation
:
~
optional
:
true
---
---
source
:
source
:
file
:
apiargs-common-option.yaml
file
:
apiargs-common-option.yaml
ref
:
session
ref
:
session
---
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
startAtOperationTime
post
:
|
.. versionadded:: 1.4
---
source
:
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
typeMap
ref
:
typeMap
...
...
docs/includes/apiargs-MongoDBCollection-method-watch-param.yaml
View file @
eb2cd59f
arg_name
:
param
source
:
name
:
$pipeline
file
:
apiargs-method-watch-param.yaml
type
:
array|object
ref
:
$pipeline
description
:
|
The pipeline of stages to append to an initial ``$changeStream`` stage.
interface
:
phpmethod
operation
:
~
optional
:
true
---
---
source
:
source
:
file
:
apiargs-
common
-param.yaml
file
:
apiargs-
method-watch
-param.yaml
ref
:
$options
ref
:
$options
...
...
docs/includes/apiargs-MongoDBDatabase-common-option.yaml
View file @
eb2cd59f
arg_name
:
option
name
:
readConcern
type
:
:php:`MongoDB\\Driver\\ReadConcern <class.mongodb-driver-readconcern>`
description
:
|
:manual:`Read concern </reference/read-concern>` to use for the operation.
Defaults to the database'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.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
readPreference
type
:
:php:`MongoDB\\Driver\\ReadPreference <class.mongodb-driver-readpreference>`
description
:
|
:manual:`Read preference </reference/read-preference>` to use for the
operation. Defaults to the database's read preference.
interface
:
phpmethod
operation
:
~
optional
:
true
---
source
:
source
:
file
:
apiargs-common-option.yaml
file
:
apiargs-common-option.yaml
ref
:
typeMap
ref
:
typeMap
...
...
docs/includes/apiargs-MongoDBDatabase-method-createCollection-option.yaml
View file @
eb2cd59f
...
@@ -28,7 +28,7 @@ operation: ~
...
@@ -28,7 +28,7 @@ operation: ~
optional
:
true
optional
:
true
---
---
source
:
source
:
file
:
apiargs-
MongoDBCollection-
common-option.yaml
file
:
apiargs-common-option.yaml
ref
:
collation
ref
:
collation
pre
:
|
pre
:
|
Specifies the :manual:`collation
Specifies the :manual:`collation
...
...
docs/includes/apiargs-MongoDBDatabase-method-watch-option.yaml
0 → 100644
View file @
eb2cd59f
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
batchSize
---
source
:
file
:
apiargs-common-option.yaml
ref
:
collation
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
fullDocument
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
maxAwaitTimeMS
---
source
:
file
:
apiargs-MongoDBDatabase-common-option.yaml
ref
:
readConcern
---
source
:
file
:
apiargs-MongoDBDatabase-common-option.yaml
ref
:
readPreference
post
:
|
This is used for both the initial change stream aggregation and for
server selection during an automatic resume.
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
resumeAfter
---
source
:
file
:
apiargs-common-option.yaml
ref
:
session
---
source
:
file
:
apiargs-method-watch-option.yaml
ref
:
startAtOperationTime
---
source
:
file
:
apiargs-MongoDBDatabase-common-option.yaml
ref
:
typeMap
...
docs/includes/apiargs-MongoDBDatabase-method-watch-param.yaml
0 → 100644
View file @
eb2cd59f
source
:
file
:
apiargs-method-watch-param.yaml
ref
:
$pipeline
---
source
:
file
:
apiargs-method-watch-param.yaml
ref
:
$options
...
docs/includes/apiargs-common-option.yaml
View file @
eb2cd59f
arg_name
:
option
arg_name
:
option
name
:
collation
type
:
array|object
description
:
|
:manual:`Collation </reference/collation>` allows users to specify
language-specific rules for string comparison, such as rules for lettercase
and accent marks. When specifying collation, the ``locale`` field is
mandatory; all other collation fields are optional. For descriptions of the
fields, see :manual:`Collation Document
</reference/collation/#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.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
maxTimeMS
name
:
maxTimeMS
type
:
integer
type
:
integer
description
:
|
description
:
|
...
...
docs/includes/apiargs-method-watch-option.yaml
0 → 100644
View file @
eb2cd59f
---
arg_name
:
option
name
:
batchSize
type
:
integer
description
:
|
Specifies the maximum number of change events to return in each batch of the
response from the MongoDB cluster.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
fullDocument
type
:
string
description
:
|
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 ``$changeStream`` pipeline stage.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
maxAwaitTimeMS
type
:
integer
description
:
|
Positive integer denoting the time limit in milliseconds for the server to
block a getMore operation if no data is available.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
resumeAfter
type
:
array|object
description
:
|
Specifies the logical starting point for the new change stream. The ``_id``
field in documents returned by the change stream may be used here.
Using this option in conjunction with ``startAtOperationTime`` will result in
a server error. The options are mutually exclusive.
.. note::
This is an option of the ``$changeStream`` pipeline stage.
interface
:
phpmethod
operation
:
~
optional
:
true
---
arg_name
:
option
name
:
startAtOperationTime
type
:
:php:`MongoDB\\BSON\\TimestampInterface <class.mongodb-bson-timestampinterface>`
description
:
|
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 ``operationTime`` that can be used here. By default, the
``operationTime`` returned by the initial ``aggregate`` command will be used
if available.
Using this option in conjunction with ``resumeAfter`` will 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 ``$changeStream`` pipeline stage.
interface
:
phpmethod
operation
:
~
optional
:
true
...
docs/includes/apiargs-method-watch-param.yaml
0 → 100644
View file @
eb2cd59f
arg_name
:
param
name
:
$pipeline
type
:
array|object
description
:
|
The pipeline of stages to append to an initial ``$changeStream`` stage.
interface
:
phpmethod
operation
:
~
optional
:
true
---
source
:
file
:
apiargs-common-param.yaml
ref
:
$options
...
docs/reference/class/MongoDBClient.txt
View file @
eb2cd59f
...
@@ -40,3 +40,4 @@ Methods
...
@@ -40,3 +40,4 @@ Methods
/reference/method/MongoDBClient-selectCollection
/reference/method/MongoDBClient-selectCollection
/reference/method/MongoDBClient-selectDatabase
/reference/method/MongoDBClient-selectDatabase
/reference/method/MongoDBClient-startSession
/reference/method/MongoDBClient-startSession
/reference/method/MongoDBClient-watch
docs/reference/class/MongoDBDatabase.txt
View file @
eb2cd59f
...
@@ -59,5 +59,6 @@ Methods
...
@@ -59,5 +59,6 @@ Methods
/reference/method/MongoDBDatabase-modifyCollection
/reference/method/MongoDBDatabase-modifyCollection
/reference/method/MongoDBDatabase-selectCollection
/reference/method/MongoDBDatabase-selectCollection
/reference/method/MongoDBDatabase-selectGridFSBucket
/reference/method/MongoDBDatabase-selectGridFSBucket
/reference/method/MongoDBDatabase-watch
/reference/method/MongoDBDatabase-withOptions
/reference/method/MongoDBDatabase-withOptions
docs/reference/method/MongoDBClient-watch.txt
0 → 100644
View file @
eb2cd59f
========================
MongoDB\\Client::watch()
========================
.. versionadded:: 1.4
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\Client::watch()
Executes a :manual:`change stream </changeStreams>` operation on the client.
The change stream can be watched for cluster-level changes.
.. code-block:: php
function watch(array $pipeline = [], array $options = []): MongoDB\ChangeStream
This method has the following parameters:
.. include:: /includes/apiargs/MongoDBClient-method-watch-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBClient-method-watch-option.rst
Return Values
-------------
A :phpclass:`MongoDB\\ChangeStream` object, which allows for iteration of
events in the change stream via the :php:`Iterator <class.iterator>` interface.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Examples
--------
This example reports events while iterating a change stream.
.. code-block:: php
<?php
$uri = 'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet';
$client = new MongoDB\Client($uri);
$changeStream = $client->watch();
for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
if ($event['operationType'] === 'invalidate') {
break;
}
$ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']);
$id = json_encode($event['documentKey']['_id']);
switch ($event['operationType']) {
case 'delete':
printf("Deleted document in %s with _id: %s\n\n", $ns, $id);
break;
case 'insert':
printf("Inserted new document in %s\n", $ns);
echo json_encode($event['fullDocument']), "\n\n";
break;
case 'replace':
printf("Replaced new document in %s with _id: %s\n", $ns, $id);
echo json_encode($event['fullDocument']), "\n\n";
break;
case 'update':
printf("Updated document in %s with _id: %s\n", $ns, $id);
echo json_encode($event['updateDescription']), "\n\n";
break;
}
}
Assuming that a document was inserted, updated, and deleted while the above
script was iterating the change stream, the output would then resemble:
.. code-block:: none
Inserted new document in app.user
{"_id":{"$oid":"5b329b6674083047cc05e607"},"username":"bob"}
Inserted new document in app.products
{"_id":{"$oid":"5b329b6a74083047cc05e608"},"name":"Widget","quantity":5}
Inserted new document in logs.messages
{"_id":{"$oid":"5b329b7374083047cc05e609"},"msg":"bob purchased a widget"}
See Also
--------
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
the MongoDB Manual
- :manual:`Change Streams </changeStreams>` documentation in the MongoDB manual
- :manual:`Change Events </reference/change-events/>` documentation in the
MongoDB manual
docs/reference/method/MongoDBCollection-watch.txt
View file @
eb2cd59f
...
@@ -18,7 +18,7 @@ Definition
...
@@ -18,7 +18,7 @@ Definition
.. phpmethod:: MongoDB\\Collection::watch()
.. phpmethod:: MongoDB\\Collection::watch()
Executes a :manual:`change stream </changeStreams>` operation on the
Executes a :manual:`change stream </changeStreams>` operation on the
collection.
collection.
The change stream can be watched for collection-level changes.
.. code-block:: php
.. code-block:: php
...
@@ -68,6 +68,10 @@ This example reports events while iterating a change stream.
...
@@ -68,6 +68,10 @@ This example reports events while iterating a change stream.
$event = $changeStream->current();
$event = $changeStream->current();
if ($event['operationType'] === 'invalidate') {
break;
}
$ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']);
$ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']);
$id = json_encode($event['documentKey']['_id']);
$id = json_encode($event['documentKey']['_id']);
...
@@ -98,13 +102,14 @@ script was iterating the change stream, the output would then resemble:
...
@@ -98,13 +102,14 @@ script was iterating the change stream, the output would then resemble:
.. code-block:: none
.. code-block:: none
Inserted new document in test.
inventory
Inserted new document in test.
user
{"_id":{"$oid":"5
a81fc0d6118fd1af1790d32"},"name":"Widget","quantity":5
}
{"_id":{"$oid":"5
b329c4874083047cc05e60a"},"username":"bob"
}
Updated document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}
Inserted new document in test.products
{"
updatedFields":{"quantity":4},"removedFields":[]
}
{"
_id":{"$oid":"5b329c4d74083047cc05e60b"},"name":"Widget","quantity":5
}
Deleted document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}
Updated document in test.user with _id: {"$oid":"5b329a4f74083047cc05e603"}
{"updatedFields":{"username":"robert"},"removedFields":[]}
See Also
See Also
--------
--------
...
...
docs/reference/method/MongoDBDatabase-watch.txt
0 → 100644
View file @
eb2cd59f
==========================
MongoDB\\Database::watch()
==========================
.. versionadded:: 1.4
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\Database::watch()
Executes a :manual:`change stream </changeStreams>` operation on the
database. The change stream can be watched for database-level changes.
.. code-block:: php
function watch(array $pipeline = [], array $options = []): MongoDB\ChangeStream
This method has the following parameters:
.. include:: /includes/apiargs/MongoDBDatabase-method-watch-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBDatabase-method-watch-option.rst
Return Values
-------------
A :phpclass:`MongoDB\\ChangeStream` object, which allows for iteration of
events in the change stream via the :php:`Iterator <class.iterator>` interface.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Examples
--------
This example reports events while iterating a change stream.
.. code-block:: php
<?php
$uri = 'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet';
$database = (new MongoDB\Client($uri))->test;
$changeStream = $database->watch();
for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
if ($event['operationType'] === 'invalidate') {
break;
}
$ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']);
$id = json_encode($event['documentKey']['_id']);
switch ($event['operationType']) {
case 'delete':
printf("Deleted document in %s with _id: %s\n\n", $ns, $id);
break;
case 'insert':
printf("Inserted new document in %s\n", $ns);
echo json_encode($event['fullDocument']), "\n\n";
break;
case 'replace':
printf("Replaced new document in %s with _id: %s\n", $ns, $id);
echo json_encode($event['fullDocument']), "\n\n";
break;
case 'update':
printf("Updated document in %s with _id: %s\n", $ns, $id);
echo json_encode($event['updateDescription']), "\n\n";
break;
}
}
Assuming that a document was inserted, updated, and deleted while the above
script was iterating the change stream, the output would then resemble:
.. code-block:: none
Inserted new document in test.inventory
{"_id":{"$oid":"5a81fc0d6118fd1af1790d32"},"name":"Widget","quantity":5}
Updated document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}
{"updatedFields":{"quantity":4},"removedFields":[]}
Deleted document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}
See Also
--------
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
the MongoDB Manual
- :manual:`Change Streams </changeStreams>` documentation in the MongoDB manual
- :manual:`Change Events </reference/change-events/>` documentation in the
MongoDB manual
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment