@@ -42,6 +42,7 @@ class Collection
42
42
'root ' => 'MongoDB\Model\BSONDocument ' ,
43
43
];
44
44
private static $ wireVersionForFindAndModifyWriteConcern = 4 ;
45
+ private static $ wireVersionForReadConcern = 4 ;
45
46
private static $ wireVersionForWritableCommandWriteConcern = 5 ;
46
47
47
48
private $ collectionName ;
@@ -169,13 +170,6 @@ public function aggregate(array $pipeline, array $options = [])
169
170
{
170
171
$ hasOutStage = \MongoDB \is_last_pipeline_operator_out ($ pipeline );
171
172
172
- /* A "majority" read concern is not compatible with the $out stage, so
173
- * avoid providing the Collection's read concern if it would conflict.
174
- */
175
- if ( ! isset ($ options ['readConcern ' ]) && ! ($ hasOutStage && $ this ->readConcern ->getLevel () === ReadConcern::MAJORITY )) {
176
- $ options ['readConcern ' ] = $ this ->readConcern ;
177
- }
178
-
179
173
if ( ! isset ($ options ['readPreference ' ])) {
180
174
$ options ['readPreference ' ] = $ this ->readPreference ;
181
175
}
@@ -184,12 +178,21 @@ public function aggregate(array $pipeline, array $options = [])
184
178
$ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
185
179
}
186
180
181
+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
182
+
183
+ /* A "majority" read concern is not compatible with the $out stage, so
184
+ * avoid providing the Collection's read concern if it would conflict.
185
+ */
186
+ if ( ! isset ($ options ['readConcern ' ]) &&
187
+ ! ($ hasOutStage && $ this ->readConcern ->getLevel () === ReadConcern::MAJORITY ) &&
188
+ \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
189
+ $ options ['readConcern ' ] = $ this ->readConcern ;
190
+ }
191
+
187
192
if ( ! isset ($ options ['typeMap ' ]) && ( ! isset ($ options ['useCursor ' ]) || $ options ['useCursor ' ])) {
188
193
$ options ['typeMap ' ] = $ this ->typeMap ;
189
194
}
190
195
191
- $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
192
-
193
196
if ($ hasOutStage && ! isset ($ options ['writeConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForWritableCommandWriteConcern )) {
194
197
$ options ['writeConcern ' ] = $ this ->writeConcern ;
195
198
}
@@ -236,17 +239,18 @@ public function bulkWrite(array $operations, array $options = [])
236
239
*/
237
240
public function count ($ filter = [], array $ options = [])
238
241
{
239
- if ( ! isset ($ options ['readConcern ' ])) {
240
- $ options ['readConcern ' ] = $ this ->readConcern ;
241
- }
242
-
243
242
if ( ! isset ($ options ['readPreference ' ])) {
244
243
$ options ['readPreference ' ] = $ this ->readPreference ;
245
244
}
246
245
247
- $ operation = new Count ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
248
246
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
249
247
248
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
249
+ $ options ['readConcern ' ] = $ this ->readConcern ;
250
+ }
251
+
252
+ $ operation = new Count ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
253
+
250
254
return $ operation ->execute ($ server );
251
255
}
252
256
@@ -374,17 +378,18 @@ public function deleteOne($filter, array $options = [])
374
378
*/
375
379
public function distinct ($ fieldName , $ filter = [], array $ options = [])
376
380
{
377
- if ( ! isset ($ options ['readConcern ' ])) {
378
- $ options ['readConcern ' ] = $ this ->readConcern ;
379
- }
380
-
381
381
if ( ! isset ($ options ['readPreference ' ])) {
382
382
$ options ['readPreference ' ] = $ this ->readPreference ;
383
383
}
384
384
385
- $ operation = new Distinct ($ this ->databaseName , $ this ->collectionName , $ fieldName , $ filter , $ options );
386
385
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
387
386
387
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
388
+ $ options ['readConcern ' ] = $ this ->readConcern ;
389
+ }
390
+
391
+ $ operation = new Distinct ($ this ->databaseName , $ this ->collectionName , $ fieldName , $ filter , $ options );
392
+
388
393
return $ operation ->execute ($ server );
389
394
}
390
395
@@ -490,20 +495,21 @@ public function dropIndexes(array $options = [])
490
495
*/
491
496
public function find ($ filter = [], array $ options = [])
492
497
{
493
- if ( ! isset ($ options ['readConcern ' ])) {
494
- $ options ['readConcern ' ] = $ this ->readConcern ;
495
- }
496
-
497
498
if ( ! isset ($ options ['readPreference ' ])) {
498
499
$ options ['readPreference ' ] = $ this ->readPreference ;
499
500
}
500
501
502
+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
503
+
504
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
505
+ $ options ['readConcern ' ] = $ this ->readConcern ;
506
+ }
507
+
501
508
if ( ! isset ($ options ['typeMap ' ])) {
502
509
$ options ['typeMap ' ] = $ this ->typeMap ;
503
510
}
504
511
505
512
$ operation = new Find ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
506
- $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
507
513
508
514
return $ operation ->execute ($ server );
509
515
}
@@ -522,14 +528,16 @@ public function find($filter = [], array $options = [])
522
528
*/
523
529
public function findOne ($ filter = [], array $ options = [])
524
530
{
525
- if ( ! isset ($ options ['readConcern ' ])) {
526
- $ options ['readConcern ' ] = $ this ->readConcern ;
527
- }
528
-
529
531
if ( ! isset ($ options ['readPreference ' ])) {
530
532
$ options ['readPreference ' ] = $ this ->readPreference ;
531
533
}
532
534
535
+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
536
+
537
+ if ( ! isset ($ options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
538
+ $ options ['readConcern ' ] = $ this ->readConcern ;
539
+ }
540
+
533
541
if ( ! isset ($ options ['typeMap ' ])) {
534
542
$ options ['typeMap ' ] = $ this ->typeMap ;
535
543
}
0 commit comments