Skip to content

Commit 689d943

Browse files
committed
Default values for fullDocument and readPreference Watch options
Refactoring the change stream tests to execute Watch directly highlighted a bug where no read preference was available during the resume process. This ensures that a read preference is always set, and also specifies a "fullDocument" default (consistent with other drivers).
1 parent d9858d1 commit 689d943

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/Operation/Watch.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ class Watch implements Executable
5151
*
5252
* Supported options:
5353
*
54-
* * fullDocument (string): Allowed values: ‘default’, ‘updateLookup’.
55-
* Defaults to ‘default’. When set to ‘updateLookup’, the change
56-
* notification for partial updates will include both a delta describing
57-
* the changes to the document, as well as a copy of the entire document
58-
* that was changed from some time after the change occurred. For forward
59-
* compatibility, a driver MUST NOT raise an error when a user provides
60-
* an unknown value. The driver relies on the server to validate this
61-
* option.
54+
* * fullDocument (string): Determines whether the "fullDocument" field
55+
* will be populated for update operations. By default, change streams
56+
* only return the delta of fields during the update operation (via the
57+
* "updateDescription" field). To additionally return the most current
58+
* majority-committed version of the updated document, specify
59+
* "updateLookup" for this option. Defaults to "default".
60+
*
61+
* Insert and replace operations always include the "fullDocument" field
62+
* and delete operations omit the field as the document no longer exists.
6263
*
6364
* * resumeAfter (document): Specifies the logical starting point for the
6465
* new change stream.
@@ -69,7 +70,9 @@ class Watch implements Executable
6970
* This is not supported for server versions < 3.2 and will result in an
7071
* exception at execution time if used.
7172
*
72-
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
73+
* * readPreference (MongoDB\Driver\ReadPreference): Read preference. This
74+
* will be used to select a new server when resuming. Defaults to a
75+
* "primary" read preference.
7376
*
7477
* * maxAwaitTimeMS (integer): The maximum amount of time for the server to
7578
* wait on new documents to satisfy a change stream query.
@@ -93,6 +96,11 @@ class Watch implements Executable
9396
*/
9497
public function __construct(Manager $manager, $databaseName, $collectionName, array $pipeline, array $options = [])
9598
{
99+
$options += [
100+
'fullDocument' => self::FULL_DOCUMENT_DEFAULT,
101+
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
102+
];
103+
96104
if (isset($options['batchSize']) && ! is_integer($options['batchSize'])) {
97105
throw InvalidArgumentException::invalidType('"batchSize" option', $options['batchSize'], 'integer');
98106
}

0 commit comments

Comments
 (0)