Skip to content

Commit 880f45b

Browse files
committed
PHPLIB-129: Support writeConcern option for findAndModify
1 parent 006f300 commit 880f45b

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

src/Collection.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
class Collection
3939
{
40+
private static $wireVersionForFindAndModifyWriteConcern = 4;
41+
4042
private $collectionName;
4143
private $databaseName;
4244
private $manager;
@@ -394,9 +396,14 @@ public function findOne($filter = [], array $options = [])
394396
*/
395397
public function findOneAndDelete($filter, array $options = [])
396398
{
397-
$operation = new FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options);
398399
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
399400

401+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
402+
$options['writeConcern'] = $this->writeConcern;
403+
}
404+
405+
$operation = new FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options);
406+
400407
return $operation->execute($server);
401408
}
402409

@@ -417,9 +424,14 @@ public function findOneAndDelete($filter, array $options = [])
417424
*/
418425
public function findOneAndReplace($filter, $replacement, array $options = [])
419426
{
420-
$operation = new FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options);
421427
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
422428

429+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
430+
$options['writeConcern'] = $this->writeConcern;
431+
}
432+
433+
$operation = new FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options);
434+
423435
return $operation->execute($server);
424436
}
425437

@@ -440,9 +452,14 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
440452
*/
441453
public function findOneAndUpdate($filter, $update, array $options = [])
442454
{
443-
$operation = new FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options);
444455
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
445456

457+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
458+
$options['writeConcern'] = $this->writeConcern;
459+
}
460+
461+
$operation = new FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options);
462+
446463
return $operation->execute($server);
447464
}
448465

src/Operation/FindAndModify.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
7+
use MongoDB\Driver\WriteConcern;
78
use MongoDB\Exception\InvalidArgumentException;
89
use MongoDB\Exception\InvalidArgumentTypeException;
910
use MongoDB\Exception\UnexpectedValueException;
@@ -20,6 +21,7 @@
2021
class FindAndModify implements Executable
2122
{
2223
private static $wireVersionForDocumentLevelValidation = 4;
24+
private static $wireVersionForWriteConcern = 4;
2325

2426
private $databaseName;
2527
private $collectionName;
@@ -58,6 +60,9 @@ class FindAndModify implements Executable
5860
* matches the query. This option is ignored for remove operations. The
5961
* default is false.
6062
*
63+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
64+
* is only supported for server versions >= 3.2.
65+
*
6166
* @param string $databaseName Database name
6267
* @param string $collectionName Collection name
6368
* @param array $options Command options
@@ -103,6 +108,10 @@ public function __construct($databaseName, $collectionName, array $options)
103108
throw new InvalidArgumentTypeException('"update" option', $options['update'], 'array or object');
104109
}
105110

111+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
112+
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
113+
}
114+
106115
if ( ! is_bool($options['upsert'])) {
107116
throw new InvalidArgumentTypeException('"upsert" option', $options['upsert'], 'boolean');
108117
}
@@ -181,6 +190,10 @@ private function createCommand(Server $server)
181190
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
182191
}
183192

193+
if (isset($this->options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
194+
$cmd['writeConcern'] = $this->options['writeConcern'];
195+
}
196+
184197
return new Command($cmd);
185198
}
186199
}

src/Operation/FindOneAndDelete.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class FindOneAndDelete implements Executable
3232
* * sort (document): Determines which document the operation modifies if
3333
* the query selects multiple documents.
3434
*
35+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
36+
* is only supported for server versions >= 3.2.
37+
*
3538
* @param string $databaseName Database name
3639
* @param string $collectionName Collection name
3740
* @param array|object $filter Query by which to filter documents

src/Operation/FindOneAndReplace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class FindOneAndReplace implements Executable
4545
* * upsert (boolean): When true, a new document is created if no document
4646
* matches the query. The default is false.
4747
*
48+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
49+
* is only supported for server versions >= 3.2.
50+
*
4851
* @param string $databaseName Database name
4952
* @param string $collectionName Collection name
5053
* @param array|object $filter Query by which to filter documents

src/Operation/FindOneAndUpdate.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class FindOneAndUpdate implements Executable
4545
* * upsert (boolean): When true, a new document is created if no document
4646
* matches the query. The default is false.
4747
*
48+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
49+
* is only supported for server versions >= 3.2.
50+
*
4851
* @param string $databaseName Database name
4952
* @param string $collectionName Collection name
5053
* @param array|object $filter Query by which to filter documents

tests/Operation/FindAndModifyTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function provideInvalidConstructorOptions()
5555
$options[][] = ['upsert' => $value];
5656
}
5757

58+
foreach ($this->getInvalidWriteConcernValues() as $value) {
59+
$options[][] = ['writeConcern' => $value];
60+
}
61+
5862
return $options;
5963
}
6064

0 commit comments

Comments
 (0)