@@ -42,6 +42,7 @@ class Collection
4242 'root ' => 'MongoDB\Model\BSONDocument ' ,
4343 ];
4444 private static $ wireVersionForFindAndModifyWriteConcern = 4 ;
45+ private static $ wireVersionForReadConcern = 4 ;
4546 private static $ wireVersionForWritableCommandWriteConcern = 5 ;
4647
4748 private $ collectionName ;
@@ -169,13 +170,6 @@ public function aggregate(array $pipeline, array $options = [])
169170 {
170171 $ hasOutStage = \MongoDB \is_last_pipeline_operator_out ($ pipeline );
171172
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-
179173 if ( ! isset ($ options ['readPreference ' ])) {
180174 $ options ['readPreference ' ] = $ this ->readPreference ;
181175 }
@@ -184,12 +178,21 @@ public function aggregate(array $pipeline, array $options = [])
184178 $ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
185179 }
186180
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+
187192 if ( ! isset ($ options ['typeMap ' ]) && ( ! isset ($ options ['useCursor ' ]) || $ options ['useCursor ' ])) {
188193 $ options ['typeMap ' ] = $ this ->typeMap ;
189194 }
190195
191- $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
192-
193196 if ($ hasOutStage && ! isset ($ options ['writeConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForWritableCommandWriteConcern )) {
194197 $ options ['writeConcern ' ] = $ this ->writeConcern ;
195198 }
@@ -236,17 +239,18 @@ public function bulkWrite(array $operations, array $options = [])
236239 */
237240 public function count ($ filter = [], array $ options = [])
238241 {
239- if ( ! isset ($ options ['readConcern ' ])) {
240- $ options ['readConcern ' ] = $ this ->readConcern ;
241- }
242-
243242 if ( ! isset ($ options ['readPreference ' ])) {
244243 $ options ['readPreference ' ] = $ this ->readPreference ;
245244 }
246245
247- $ operation = new Count ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
248246 $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
249247
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+
250254 return $ operation ->execute ($ server );
251255 }
252256
@@ -374,17 +378,18 @@ public function deleteOne($filter, array $options = [])
374378 */
375379 public function distinct ($ fieldName , $ filter = [], array $ options = [])
376380 {
377- if ( ! isset ($ options ['readConcern ' ])) {
378- $ options ['readConcern ' ] = $ this ->readConcern ;
379- }
380-
381381 if ( ! isset ($ options ['readPreference ' ])) {
382382 $ options ['readPreference ' ] = $ this ->readPreference ;
383383 }
384384
385- $ operation = new Distinct ($ this ->databaseName , $ this ->collectionName , $ fieldName , $ filter , $ options );
386385 $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
387386
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+
388393 return $ operation ->execute ($ server );
389394 }
390395
@@ -490,20 +495,21 @@ public function dropIndexes(array $options = [])
490495 */
491496 public function find ($ filter = [], array $ options = [])
492497 {
493- if ( ! isset ($ options ['readConcern ' ])) {
494- $ options ['readConcern ' ] = $ this ->readConcern ;
495- }
496-
497498 if ( ! isset ($ options ['readPreference ' ])) {
498499 $ options ['readPreference ' ] = $ this ->readPreference ;
499500 }
500501
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+
501508 if ( ! isset ($ options ['typeMap ' ])) {
502509 $ options ['typeMap ' ] = $ this ->typeMap ;
503510 }
504511
505512 $ operation = new Find ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
506- $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
507513
508514 return $ operation ->execute ($ server );
509515 }
@@ -522,14 +528,16 @@ public function find($filter = [], array $options = [])
522528 */
523529 public function findOne ($ filter = [], array $ options = [])
524530 {
525- if ( ! isset ($ options ['readConcern ' ])) {
526- $ options ['readConcern ' ] = $ this ->readConcern ;
527- }
528-
529531 if ( ! isset ($ options ['readPreference ' ])) {
530532 $ options ['readPreference ' ] = $ this ->readPreference ;
531533 }
532534
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+
533541 if ( ! isset ($ options ['typeMap ' ])) {
534542 $ options ['typeMap ' ] = $ this ->typeMap ;
535543 }
0 commit comments