@@ -40,6 +40,7 @@ class Collection
40
40
'root ' => 'MongoDB\Model\BSONDocument ' ,
41
41
];
42
42
private static $ wireVersionForFindAndModifyWriteConcern = 4 ;
43
+ private static $ wireVersionForReadConcern = 4 ;
43
44
44
45
private $ collectionName ;
45
46
private $ databaseName ;
@@ -162,13 +163,6 @@ public function aggregate(array $pipeline, array $options = [])
162
163
{
163
164
$ hasOutStage = \MongoDB \is_last_pipeline_operator_out ($ pipeline );
164
165
165
- /* A "majority" read concern is not compatible with the $out stage, so
166
- * avoid providing the Collection's read concern if it would conflict.
167
- */
168
- if ( ! isset ($ options ['readConcern ' ]) && ! ($ hasOutStage && $ this ->readConcern ->getLevel () === ReadConcern::MAJORITY )) {
169
- $ options ['readConcern ' ] = $ this ->readConcern ;
170
- }
171
-
172
166
if ( ! isset ($ options ['readPreference ' ])) {
173
167
$ options ['readPreference ' ] = $ this ->readPreference ;
174
168
}
@@ -177,12 +171,22 @@ public function aggregate(array $pipeline, array $options = [])
177
171
$ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
178
172
}
179
173
174
+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
175
+
176
+ /* A "majority" read concern is not compatible with the $out stage, so
177
+ * avoid providing the Collection's read concern if it would conflict.
178
+ */
179
+ if ( ! isset ($ options ['readConcern ' ]) &&
180
+ ! ($ hasOutStage && $ this ->readConcern ->getLevel () === ReadConcern::MAJORITY ) &&
181
+ \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
182
+ $ options ['readConcern ' ] = $ this ->readConcern ;
183
+ }
184
+
180
185
if ( ! isset ($ options ['typeMap ' ]) && ( ! isset ($ options ['useCursor ' ]) || $ options ['useCursor ' ])) {
181
186
$ options ['typeMap ' ] = $ this ->typeMap ;
182
187
}
183
188
184
189
$ operation = new Aggregate ($ this ->databaseName , $ this ->collectionName , $ pipeline , $ options );
185
- $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
186
190
187
191
return $ operation ->execute ($ server );
188
192
}
@@ -217,17 +221,18 @@ public function bulkWrite(array $operations, array $options = [])
217
221
*/
218
222
public function count ($ filter = [], array $ options = [])
219
223
{
220
- if ( ! isset ($ options ['readConcern ' ])) {
221
- $ options ['readConcern ' ] = $ this ->readConcern ;
222
- }
223
-
224
224
if ( ! isset ($ options ['readPreference ' ])) {
225
225
$ options ['readPreference ' ] = $ this ->readPreference ;
226
226
}
227
227
228
- $ operation = new Count ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
229
228
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
230
229
230
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
231
+ $ options ['readConcern ' ] = $ this ->readConcern ;
232
+ }
233
+
234
+ $ operation = new Count ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
235
+
231
236
return $ operation ->execute ($ server );
232
237
}
233
238
@@ -329,17 +334,18 @@ public function deleteOne($filter, array $options = [])
329
334
*/
330
335
public function distinct ($ fieldName , $ filter = [], array $ options = [])
331
336
{
332
- if ( ! isset ($ options ['readConcern ' ])) {
333
- $ options ['readConcern ' ] = $ this ->readConcern ;
334
- }
335
-
336
337
if ( ! isset ($ options ['readPreference ' ])) {
337
338
$ options ['readPreference ' ] = $ this ->readPreference ;
338
339
}
339
340
340
- $ operation = new Distinct ($ this ->databaseName , $ this ->collectionName , $ fieldName , $ filter , $ options );
341
341
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
342
342
343
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
344
+ $ options ['readConcern ' ] = $ this ->readConcern ;
345
+ }
346
+
347
+ $ operation = new Distinct ($ this ->databaseName , $ this ->collectionName , $ fieldName , $ filter , $ options );
348
+
343
349
return $ operation ->execute ($ server );
344
350
}
345
351
@@ -419,20 +425,21 @@ public function dropIndexes(array $options = [])
419
425
*/
420
426
public function find ($ filter = [], array $ options = [])
421
427
{
422
- if ( ! isset ($ options ['readConcern ' ])) {
423
- $ options ['readConcern ' ] = $ this ->readConcern ;
424
- }
425
-
426
428
if ( ! isset ($ options ['readPreference ' ])) {
427
429
$ options ['readPreference ' ] = $ this ->readPreference ;
428
430
}
429
431
432
+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
433
+
434
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
435
+ $ options ['readConcern ' ] = $ this ->readConcern ;
436
+ }
437
+
430
438
if ( ! isset ($ options ['typeMap ' ])) {
431
439
$ options ['typeMap ' ] = $ this ->typeMap ;
432
440
}
433
441
434
442
$ operation = new Find ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
435
- $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
436
443
437
444
return $ operation ->execute ($ server );
438
445
}
@@ -448,14 +455,16 @@ public function find($filter = [], array $options = [])
448
455
*/
449
456
public function findOne ($ filter = [], array $ options = [])
450
457
{
451
- if ( ! isset ($ options ['readConcern ' ])) {
452
- $ options ['readConcern ' ] = $ this ->readConcern ;
453
- }
454
-
455
458
if ( ! isset ($ options ['readPreference ' ])) {
456
459
$ options ['readPreference ' ] = $ this ->readPreference ;
457
460
}
458
461
462
+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
463
+
464
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
465
+ $ options ['readConcern ' ] = $ this ->readConcern ;
466
+ }
467
+
459
468
if ( ! isset ($ options ['typeMap ' ])) {
460
469
$ options ['typeMap ' ] = $ this ->typeMap ;
461
470
}
0 commit comments