@@ -66,8 +66,8 @@ public function __construct(Manager $manager, $namespace, WriteConcern $writeCon
6666 $ this ->collectionName = $ parts [1 ];
6767
6868 $ this ->manager = $ manager ;
69- $ this ->writeConcern = $ writeConcern ;
70- $ this ->readPreference = $ readPreference ;
69+ $ this ->writeConcern = $ writeConcern ?: \ MongoDB \get_manager_write_concern ( $ this -> manager ) ;
70+ $ this ->readPreference = $ readPreference ?: \ MongoDB \get_manager_read_preference ( $ this -> manager ) ;
7171 }
7272
7373 /**
@@ -95,9 +95,16 @@ public function __toString()
9595 */
9696 public function aggregate (array $ pipeline , array $ options = array ())
9797 {
98- $ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
99- $ server = $ this ->manager ->selectServer ($ readPreference );
98+ if ( ! isset ($ options ['readPreference ' ])) {
99+ $ options ['readPreference ' ] = $ this ->readPreference ;
100+ }
101+
102+ if (\MongoDB \is_last_pipeline_operator_out ($ pipeline )) {
103+ $ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
104+ }
105+
100106 $ operation = new Aggregate ($ this ->databaseName , $ this ->collectionName , $ pipeline , $ options );
107+ $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
101108
102109 return $ operation ->execute ($ server );
103110 }
@@ -112,7 +119,7 @@ public function aggregate(array $pipeline, array $options = array())
112119 */
113120 public function bulkWrite (array $ operations , array $ options = array ())
114121 {
115- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
122+ if ( ! isset ($ options ['writeConcern ' ])) {
116123 $ options ['writeConcern ' ] = $ this ->writeConcern ;
117124 }
118125
@@ -132,8 +139,12 @@ public function bulkWrite(array $operations, array $options = array())
132139 */
133140 public function count ($ filter = array (), array $ options = array ())
134141 {
142+ if ( ! isset ($ options ['readPreference ' ])) {
143+ $ options ['readPreference ' ] = $ this ->readPreference ;
144+ }
145+
135146 $ operation = new Count ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
136- $ server = $ this ->manager ->selectServer (new ReadPreference (ReadPreference:: RP_PRIMARY ) );
147+ $ server = $ this ->manager ->selectServer ($ options [ ' readPreference ' ] );
137148
138149 return $ operation ->execute ($ server );
139150 }
@@ -194,7 +205,7 @@ public function createIndexes(array $indexes)
194205 */
195206 public function deleteMany ($ filter , array $ options = array ())
196207 {
197- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
208+ if ( ! isset ($ options ['writeConcern ' ])) {
198209 $ options ['writeConcern ' ] = $ this ->writeConcern ;
199210 }
200211
@@ -215,7 +226,7 @@ public function deleteMany($filter, array $options = array())
215226 */
216227 public function deleteOne ($ filter , array $ options = array ())
217228 {
218- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
229+ if ( ! isset ($ options ['writeConcern ' ])) {
219230 $ options ['writeConcern ' ] = $ this ->writeConcern ;
220231 }
221232
@@ -236,8 +247,12 @@ public function deleteOne($filter, array $options = array())
236247 */
237248 public function distinct ($ fieldName , $ filter = array (), array $ options = array ())
238249 {
250+ if ( ! isset ($ options ['readPreference ' ])) {
251+ $ options ['readPreference ' ] = $ this ->readPreference ;
252+ }
253+
239254 $ operation = new Distinct ($ this ->databaseName , $ this ->collectionName , $ fieldName , $ filter , $ options );
240- $ server = $ this ->manager ->selectServer (new ReadPreference (ReadPreference:: RP_PRIMARY ) );
255+ $ server = $ this ->manager ->selectServer ($ options [ ' readPreference ' ] );
241256
242257 return $ operation ->execute ($ server );
243258 }
@@ -300,8 +315,12 @@ public function dropIndexes()
300315 */
301316 public function find ($ filter = array (), array $ options = array ())
302317 {
318+ if ( ! isset ($ options ['readPreference ' ])) {
319+ $ options ['readPreference ' ] = $ this ->readPreference ;
320+ }
321+
303322 $ operation = new Find ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
304- $ server = $ this ->manager ->selectServer (new ReadPreference (ReadPreference:: RP_PRIMARY ) );
323+ $ server = $ this ->manager ->selectServer ($ options [ ' readPreference ' ] );
305324
306325 return $ operation ->execute ($ server );
307326 }
@@ -317,8 +336,12 @@ public function find($filter = array(), array $options = array())
317336 */
318337 public function findOne ($ filter = array (), array $ options = array ())
319338 {
339+ if ( ! isset ($ options ['readPreference ' ])) {
340+ $ options ['readPreference ' ] = $ this ->readPreference ;
341+ }
342+
320343 $ operation = new FindOne ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
321- $ server = $ this ->manager ->selectServer (new ReadPreference (ReadPreference:: RP_PRIMARY ) );
344+ $ server = $ this ->manager ->selectServer ($ options [ ' readPreference ' ] );
322345
323346 return $ operation ->execute ($ server );
324347 }
@@ -430,7 +453,7 @@ public function getNamespace()
430453 */
431454 public function insertMany (array $ documents , array $ options = array ())
432455 {
433- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
456+ if ( ! isset ($ options ['writeConcern ' ])) {
434457 $ options ['writeConcern ' ] = $ this ->writeConcern ;
435458 }
436459
@@ -451,7 +474,7 @@ public function insertMany(array $documents, array $options = array())
451474 */
452475 public function insertOne ($ document , array $ options = array ())
453476 {
454- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
477+ if ( ! isset ($ options ['writeConcern ' ])) {
455478 $ options ['writeConcern ' ] = $ this ->writeConcern ;
456479 }
457480
@@ -487,7 +510,7 @@ public function listIndexes(array $options = array())
487510 */
488511 public function replaceOne ($ filter , $ replacement , array $ options = array ())
489512 {
490- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
513+ if ( ! isset ($ options ['writeConcern ' ])) {
491514 $ options ['writeConcern ' ] = $ this ->writeConcern ;
492515 }
493516
@@ -509,7 +532,7 @@ public function replaceOne($filter, $replacement, array $options = array())
509532 */
510533 public function updateMany ($ filter , $ update , array $ options = array ())
511534 {
512- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
535+ if ( ! isset ($ options ['writeConcern ' ])) {
513536 $ options ['writeConcern ' ] = $ this ->writeConcern ;
514537 }
515538
@@ -531,7 +554,7 @@ public function updateMany($filter, $update, array $options = array())
531554 */
532555 public function updateOne ($ filter , $ update , array $ options = array ())
533556 {
534- if ( ! isset ($ options ['writeConcern ' ]) && isset ( $ this -> writeConcern ) ) {
557+ if ( ! isset ($ options ['writeConcern ' ])) {
535558 $ options ['writeConcern ' ] = $ this ->writeConcern ;
536559 }
537560
0 commit comments