@@ -219,15 +219,15 @@ public function aggregate(array $pipeline, array $options = [])
219219 {
220220 $ hasWriteStage = is_last_pipeline_operator_write ($ pipeline );
221221
222- if (! isset ($ options ['readPreference ' ])) {
222+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
223223 $ options ['readPreference ' ] = $ this ->readPreference ;
224224 }
225225
226226 if ($ hasWriteStage ) {
227227 $ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
228228 }
229229
230- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
230+ $ server = select_server ($ this ->manager , $ options );
231231
232232 /* MongoDB 4.2 and later supports a read concern when an $out stage is
233233 * being used, but earlier versions do not.
@@ -276,7 +276,7 @@ public function bulkWrite(array $operations, array $options = [])
276276 }
277277
278278 $ operation = new BulkWrite ($ this ->databaseName , $ this ->collectionName , $ operations , $ options );
279- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
279+ $ server = select_server ($ this ->manager , $ options );
280280
281281 return $ operation ->execute ($ server );
282282 }
@@ -297,11 +297,11 @@ public function bulkWrite(array $operations, array $options = [])
297297 */
298298 public function count ($ filter = [], array $ options = [])
299299 {
300- if (! isset ($ options ['readPreference ' ])) {
300+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
301301 $ options ['readPreference ' ] = $ this ->readPreference ;
302302 }
303303
304- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
304+ $ server = select_server ($ this ->manager , $ options );
305305
306306 if (! isset ($ options ['readConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForReadConcern ) && ! is_in_transaction ($ options )) {
307307 $ options ['readConcern ' ] = $ this ->readConcern ;
@@ -326,11 +326,11 @@ public function count($filter = [], array $options = [])
326326 */
327327 public function countDocuments ($ filter = [], array $ options = [])
328328 {
329- if (! isset ($ options ['readPreference ' ])) {
329+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
330330 $ options ['readPreference ' ] = $ this ->readPreference ;
331331 }
332332
333- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
333+ $ server = select_server ($ this ->manager , $ options );
334334
335335 if (! isset ($ options ['readConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForReadConcern ) && ! is_in_transaction ($ options )) {
336336 $ options ['readConcern ' ] = $ this ->readConcern ;
@@ -392,7 +392,7 @@ public function createIndex($key, array $options = [])
392392 */
393393 public function createIndexes (array $ indexes , array $ options = [])
394394 {
395- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
395+ $ server = select_server ($ this ->manager , $ options );
396396
397397 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForWritableCommandWriteConcern ) && ! is_in_transaction ($ options )) {
398398 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -422,7 +422,7 @@ public function deleteMany($filter, array $options = [])
422422 }
423423
424424 $ operation = new DeleteMany ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
425- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
425+ $ server = select_server ($ this ->manager , $ options );
426426
427427 return $ operation ->execute ($ server );
428428 }
@@ -446,7 +446,7 @@ public function deleteOne($filter, array $options = [])
446446 }
447447
448448 $ operation = new DeleteOne ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
449- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
449+ $ server = select_server ($ this ->manager , $ options );
450450
451451 return $ operation ->execute ($ server );
452452 }
@@ -466,11 +466,11 @@ public function deleteOne($filter, array $options = [])
466466 */
467467 public function distinct ($ fieldName , $ filter = [], array $ options = [])
468468 {
469- if (! isset ($ options ['readPreference ' ])) {
469+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
470470 $ options ['readPreference ' ] = $ this ->readPreference ;
471471 }
472472
473- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
473+ $ server = select_server ($ this ->manager , $ options );
474474
475475 if (! isset ($ options ['readConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForReadConcern ) && ! is_in_transaction ($ options )) {
476476 $ options ['readConcern ' ] = $ this ->readConcern ;
@@ -497,7 +497,7 @@ public function drop(array $options = [])
497497 $ options ['typeMap ' ] = $ this ->typeMap ;
498498 }
499499
500- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
500+ $ server = select_server ($ this ->manager , $ options );
501501
502502 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForWritableCommandWriteConcern ) && ! is_in_transaction ($ options )) {
503503 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -531,7 +531,7 @@ public function dropIndex($indexName, array $options = [])
531531 $ options ['typeMap ' ] = $ this ->typeMap ;
532532 }
533533
534- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
534+ $ server = select_server ($ this ->manager , $ options );
535535
536536 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForWritableCommandWriteConcern ) && ! is_in_transaction ($ options )) {
537537 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -558,7 +558,7 @@ public function dropIndexes(array $options = [])
558558 $ options ['typeMap ' ] = $ this ->typeMap ;
559559 }
560560
561- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
561+ $ server = select_server ($ this ->manager , $ options );
562562
563563 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForWritableCommandWriteConcern ) && ! is_in_transaction ($ options )) {
564564 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -582,11 +582,11 @@ public function dropIndexes(array $options = [])
582582 */
583583 public function estimatedDocumentCount (array $ options = [])
584584 {
585- if (! isset ($ options ['readPreference ' ])) {
585+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
586586 $ options ['readPreference ' ] = $ this ->readPreference ;
587587 }
588588
589- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
589+ $ server = select_server ($ this ->manager , $ options );
590590
591591 if (! isset ($ options ['readConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForReadConcern ) && ! is_in_transaction ($ options )) {
592592 $ options ['readConcern ' ] = $ this ->readConcern ;
@@ -611,15 +611,15 @@ public function estimatedDocumentCount(array $options = [])
611611 */
612612 public function explain (Explainable $ explainable , array $ options = [])
613613 {
614- if (! isset ($ options ['readPreference ' ])) {
614+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
615615 $ options ['readPreference ' ] = $ this ->readPreference ;
616616 }
617617
618618 if (! isset ($ options ['typeMap ' ])) {
619619 $ options ['typeMap ' ] = $ this ->typeMap ;
620620 }
621621
622- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
622+ $ server = select_server ($ this ->manager , $ options );
623623
624624 $ operation = new Explain ($ this ->databaseName , $ explainable , $ options );
625625
@@ -640,11 +640,11 @@ public function explain(Explainable $explainable, array $options = [])
640640 */
641641 public function find ($ filter = [], array $ options = [])
642642 {
643- if (! isset ($ options ['readPreference ' ])) {
643+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
644644 $ options ['readPreference ' ] = $ this ->readPreference ;
645645 }
646646
647- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
647+ $ server = select_server ($ this ->manager , $ options );
648648
649649 if (! isset ($ options ['readConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForReadConcern ) && ! is_in_transaction ($ options )) {
650650 $ options ['readConcern ' ] = $ this ->readConcern ;
@@ -673,11 +673,11 @@ public function find($filter = [], array $options = [])
673673 */
674674 public function findOne ($ filter = [], array $ options = [])
675675 {
676- if (! isset ($ options ['readPreference ' ])) {
676+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
677677 $ options ['readPreference ' ] = $ this ->readPreference ;
678678 }
679679
680- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
680+ $ server = select_server ($ this ->manager , $ options );
681681
682682 if (! isset ($ options ['readConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForReadConcern ) && ! is_in_transaction ($ options )) {
683683 $ options ['readConcern ' ] = $ this ->readConcern ;
@@ -709,7 +709,7 @@ public function findOne($filter = [], array $options = [])
709709 */
710710 public function findOneAndDelete ($ filter , array $ options = [])
711711 {
712- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
712+ $ server = select_server ($ this ->manager , $ options );
713713
714714 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForFindAndModifyWriteConcern ) && ! is_in_transaction ($ options )) {
715715 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -746,7 +746,7 @@ public function findOneAndDelete($filter, array $options = [])
746746 */
747747 public function findOneAndReplace ($ filter , $ replacement , array $ options = [])
748748 {
749- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
749+ $ server = select_server ($ this ->manager , $ options );
750750
751751 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForFindAndModifyWriteConcern ) && ! is_in_transaction ($ options )) {
752752 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -783,7 +783,7 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
783783 */
784784 public function findOneAndUpdate ($ filter , $ update , array $ options = [])
785785 {
786- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
786+ $ server = select_server ($ this ->manager , $ options );
787787
788788 if (! isset ($ options ['writeConcern ' ]) && server_supports_feature ($ server , self ::$ wireVersionForFindAndModifyWriteConcern ) && ! is_in_transaction ($ options )) {
789789 $ options ['writeConcern ' ] = $ this ->writeConcern ;
@@ -899,7 +899,7 @@ public function insertMany(array $documents, array $options = [])
899899 }
900900
901901 $ operation = new InsertMany ($ this ->databaseName , $ this ->collectionName , $ documents , $ options );
902- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
902+ $ server = select_server ($ this ->manager , $ options );
903903
904904 return $ operation ->execute ($ server );
905905 }
@@ -922,7 +922,7 @@ public function insertOne($document, array $options = [])
922922 }
923923
924924 $ operation = new InsertOne ($ this ->databaseName , $ this ->collectionName , $ document , $ options );
925- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
925+ $ server = select_server ($ this ->manager , $ options );
926926
927927 return $ operation ->execute ($ server );
928928 }
@@ -939,7 +939,7 @@ public function insertOne($document, array $options = [])
939939 public function listIndexes (array $ options = [])
940940 {
941941 $ operation = new ListIndexes ($ this ->databaseName , $ this ->collectionName , $ options );
942- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
942+ $ server = select_server ($ this ->manager , $ options );
943943
944944 return $ operation ->execute ($ server );
945945 }
@@ -963,7 +963,7 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
963963 {
964964 $ hasOutputCollection = ! is_mapreduce_output_inline ($ out );
965965
966- if (! isset ($ options ['readPreference ' ])) {
966+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
967967 $ options ['readPreference ' ] = $ this ->readPreference ;
968968 }
969969
@@ -972,7 +972,7 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
972972 $ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
973973 }
974974
975- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
975+ $ server = select_server ($ this ->manager , $ options );
976976
977977 /* A "majority" read concern is not compatible with inline output, so
978978 * avoid providing the Collection's read concern if it would conflict.
@@ -1016,7 +1016,7 @@ public function replaceOne($filter, $replacement, array $options = [])
10161016 }
10171017
10181018 $ operation = new ReplaceOne ($ this ->databaseName , $ this ->collectionName , $ filter , $ replacement , $ options );
1019- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
1019+ $ server = select_server ($ this ->manager , $ options );
10201020
10211021 return $ operation ->execute ($ server );
10221022 }
@@ -1041,7 +1041,7 @@ public function updateMany($filter, $update, array $options = [])
10411041 }
10421042
10431043 $ operation = new UpdateMany ($ this ->databaseName , $ this ->collectionName , $ filter , $ update , $ options );
1044- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
1044+ $ server = select_server ($ this ->manager , $ options );
10451045
10461046 return $ operation ->execute ($ server );
10471047 }
@@ -1066,7 +1066,7 @@ public function updateOne($filter, $update, array $options = [])
10661066 }
10671067
10681068 $ operation = new UpdateOne ($ this ->databaseName , $ this ->collectionName , $ filter , $ update , $ options );
1069- $ server = select_server ($ this ->manager , new ReadPreference (ReadPreference:: RP_PRIMARY ), extract_session_from_options ( $ options) );
1069+ $ server = select_server ($ this ->manager , $ options );
10701070
10711071 return $ operation ->execute ($ server );
10721072 }
@@ -1082,11 +1082,11 @@ public function updateOne($filter, $update, array $options = [])
10821082 */
10831083 public function watch (array $ pipeline = [], array $ options = [])
10841084 {
1085- if (! isset ($ options ['readPreference ' ])) {
1085+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ( $ options ) ) {
10861086 $ options ['readPreference ' ] = $ this ->readPreference ;
10871087 }
10881088
1089- $ server = select_server ($ this ->manager , $ options[ ' readPreference ' ], extract_session_from_options ( $ options ) );
1089+ $ server = select_server ($ this ->manager , $ options );
10901090
10911091 /* Although change streams require a newer version of the server than
10921092 * read concerns, perform the usual wire version check before inheriting
0 commit comments