@@ -213,25 +213,17 @@ public function aggregate(array $pipeline, array $options = [])
213
213
{
214
214
$ hasWriteStage = is_last_pipeline_operator_write ($ pipeline );
215
215
216
- if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ($ options )) {
217
- $ options ['readPreference ' ] = $ this ->readPreference ;
218
- }
216
+ $ options = $ this ->inheritReadPreference ($ options );
219
217
220
218
$ server = $ hasWriteStage
221
219
? select_server_for_aggregate_write_stage ($ this ->manager , $ options )
222
220
: select_server ($ this ->manager , $ options );
223
221
224
222
/* MongoDB 4.2 and later supports a read concern when an $out stage is
225
223
* being used, but earlier versions do not.
226
- *
227
- * A read concern is also not compatible with transactions.
228
224
*/
229
- if (
230
- ! isset ($ options ['readConcern ' ]) &&
231
- ! is_in_transaction ($ options ) &&
232
- ( ! $ hasWriteStage || server_supports_feature ($ server , self ::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE ))
233
- ) {
234
- $ options ['readConcern ' ] = $ this ->readConcern ;
225
+ if (! $ hasWriteStage || server_supports_feature ($ server , self ::WIRE_VERSION_FOR_READ_CONCERN_WITH_WRITE_STAGE )) {
226
+ $ options = $ this ->inheritReadConcern ($ options );
235
227
}
236
228
237
229
$ options = $ this ->inheritCodecOrTypeMap ($ options );
@@ -543,10 +535,7 @@ public function estimatedDocumentCount(array $options = [])
543
535
*/
544
536
public function explain (Explainable $ explainable , array $ options = [])
545
537
{
546
- if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ($ options )) {
547
- $ options ['readPreference ' ] = $ this ->readPreference ;
548
- }
549
-
538
+ $ options = $ this ->inheritReadPreference ($ options );
550
539
$ options = $ this ->inheritTypeMap ($ options );
551
540
552
541
$ operation = new Explain ($ this ->databaseName , $ explainable , $ options );
@@ -842,22 +831,18 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
842
831
{
843
832
$ hasOutputCollection = ! is_mapreduce_output_inline ($ out );
844
833
845
- if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ($ options )) {
846
- $ options ['readPreference ' ] = $ this ->readPreference ;
847
- }
848
-
849
834
// Check if the out option is inline because we will want to coerce a primary read preference if not
850
835
if ($ hasOutputCollection ) {
851
836
$ options ['readPreference ' ] = new ReadPreference (ReadPreference::PRIMARY );
837
+ } else {
838
+ $ options = $ this ->inheritReadPreference ($ options );
852
839
}
853
840
854
841
/* A "majority" read concern is not compatible with inline output, so
855
842
* avoid providing the Collection's read concern if it would conflict.
856
- *
857
- * A read concern is also not compatible with transactions.
858
843
*/
859
- if (! isset ( $ options [ ' readConcern ' ]) && ! ( $ hasOutputCollection && $ this ->readConcern ->getLevel () === ReadConcern::MAJORITY ) && ! is_in_transaction ( $ options ) ) {
860
- $ options[ ' readConcern ' ] = $ this ->readConcern ;
844
+ if (! $ hasOutputCollection || $ this ->readConcern ->getLevel () !== ReadConcern::MAJORITY ) {
845
+ $ options = $ this ->inheritReadConcern ( $ options ) ;
861
846
}
862
847
863
848
$ options = $ this ->inheritWriteOptions ($ options );
@@ -1035,17 +1020,28 @@ private function inheritCodecOrTypeMap(array $options): array
1035
1020
return $ options ;
1036
1021
}
1037
1022
1038
- private function inheritReadOptions (array $ options ): array
1023
+ private function inheritReadConcern (array $ options ): array
1039
1024
{
1040
1025
// ReadConcern and ReadPreference may not change within a transaction
1041
- if (! is_in_transaction ($ options )) {
1042
- if (! isset ($ options ['readConcern ' ])) {
1043
- $ options ['readConcern ' ] = $ this ->readConcern ;
1044
- }
1026
+ if (! isset ($ options ['readConcern ' ]) && ! is_in_transaction ($ options )) {
1027
+ $ options ['readConcern ' ] = $ this ->readConcern ;
1028
+ }
1045
1029
1046
- if (! isset ($ options ['readPreference ' ])) {
1047
- $ options ['readPreference ' ] = $ this ->readPreference ;
1048
- }
1030
+ return $ options ;
1031
+ }
1032
+
1033
+ private function inheritReadOptions (array $ options ): array
1034
+ {
1035
+ $ options = $ this ->inheritReadConcern ($ options );
1036
+
1037
+ return $ this ->inheritReadPreference ($ options );
1038
+ }
1039
+
1040
+ private function inheritReadPreference (array $ options ): array
1041
+ {
1042
+ // ReadConcern and ReadPreference may not change within a transaction
1043
+ if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ($ options )) {
1044
+ $ options ['readPreference ' ] = $ this ->readPreference ;
1049
1045
}
1050
1046
1051
1047
return $ options ;
0 commit comments