Skip to content

Commit bd4eba6

Browse files
committed
PHPLIB-413: Add support for read concerns in aggregations with an $out stage
1 parent 318557b commit bd4eba6

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/Collection.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Collection
6969
private static $wireVersionForFindAndModifyWriteConcern = 4;
7070
private static $wireVersionForReadConcern = 4;
7171
private static $wireVersionForWritableCommandWriteConcern = 5;
72+
private static $wireVersionForReadConcernWithOutStage = 8;
7273

7374
private $collectionName;
7475
private $databaseName;
@@ -201,15 +202,16 @@ public function aggregate(array $pipeline, array $options = [])
201202

202203
$server = $this->manager->selectServer($options['readPreference']);
203204

204-
/* A "majority" read concern is not compatible with the $out stage, so
205-
* avoid providing the Collection's read concern if it would conflict.
205+
/* MongoDB 4.2 and later supports a read concern when an $out stage is
206+
* being used, but earlier versions do not.
206207
*
207208
* A read concern is also not compatible with transactions.
208209
*/
209210
if ( ! isset($options['readConcern']) &&
210-
! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY) &&
211211
\MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) &&
212-
! \MongoDB\is_in_transaction($options)) {
212+
! \MongoDB\is_in_transaction($options) &&
213+
( ! $hasOutStage || \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcernWithOutStage))
214+
) {
213215
$options['readConcern'] = $this->readConcern;
214216
}
215217

src/Database.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Database
4848
];
4949
private static $wireVersionForReadConcern = 4;
5050
private static $wireVersionForWritableCommandWriteConcern = 5;
51+
private static $wireVersionForReadConcernWithOutStage = 8;
5152

5253
private $databaseName;
5354
private $manager;
@@ -186,15 +187,16 @@ public function aggregate(array $pipeline, array $options = [])
186187

187188
$server = $this->manager->selectServer($options['readPreference']);
188189

189-
/* A "majority" read concern is not compatible with the $out stage, so
190-
* avoid providing the Collection's read concern if it would conflict.
190+
/* MongoDB 4.2 and later supports a read concern when an $out stage is
191+
* being used, but earlier versions do not.
191192
*
192193
* A read concern is also not compatible with transactions.
193194
*/
194195
if ( ! isset($options['readConcern']) &&
195-
! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY) &&
196196
\MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) &&
197-
! \MongoDB\is_in_transaction($options)) {
197+
! \MongoDB\is_in_transaction($options) &&
198+
( ! $hasOutStage || \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcernWithOutStage))
199+
) {
198200
$options['readConcern'] = $this->readConcern;
199201
}
200202

tests/SpecTests/Context.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public static function fromCrud(stdClass $test, $databaseName, $collectionName)
5959

6060
$clientOptions = isset($test->clientOptions) ? (array) $test->clientOptions : [];
6161

62+
if (isset($test->outcome->collection->name)) {
63+
$o->outcomeCollectionName = $test->outcome->collection->name;
64+
}
65+
6266
$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions);
6367

6468
return $o;

tests/SpecTests/CrudSpecTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ class CrudSpecTest extends FunctionalTestCase
1919
'aggregate-merge: Aggregate with $merge and majority readConcern' => 'PHPLIB-438',
2020
'aggregate-merge: Aggregate with $merge and local readConcern' => 'PHPLIB-438',
2121
'aggregate-merge: Aggregate with $merge and available readConcern' => 'PHPLIB-438',
22-
'aggregate-out-readConcern: readConcern majority with out stage' => 'PHPLIB-431',
23-
'aggregate-out-readConcern: readConcern local with out stage' => 'PHPLIB-431',
24-
'aggregate-out-readConcern: readConcern available with out stage' => 'PHPLIB-431',
25-
'aggregate-out-readConcern: readConcern linearizable with out stage' => 'PHPLIB-431',
26-
'aggregate-out-readConcern: invalid readConcern with out stage' => 'PHPLIB-431',
2722
'bulkWrite-arrayFilters: BulkWrite with arrayFilters' => 'Fails due to command assertions',
2823
'updateWithPipelines: UpdateOne using pipelines' => 'PHPLIB-418',
2924
'updateWithPipelines: UpdateMany using pipelines' => 'PHPLIB-418',

0 commit comments

Comments
 (0)