55use MongoDB \Driver \Command ;
66use MongoDB \Driver \Cursor ;
77use MongoDB \Driver \Manager ;
8+ use MongoDB \Driver \ReadConcern ;
89use MongoDB \Driver \ReadPreference ;
910use MongoDB \Driver \Server ;
1011use MongoDB \Driver \WriteConcern ;
@@ -42,6 +43,7 @@ class Collection
4243 private $ collectionName ;
4344 private $ databaseName ;
4445 private $ manager ;
46+ private $ readConcern ;
4547 private $ readPreference ;
4648 private $ writeConcern ;
4749
@@ -53,6 +55,9 @@ class Collection
5355 *
5456 * Supported options:
5557 *
58+ * * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
59+ * use for collection operations. Defaults to the Manager's read concern.
60+ *
5661 * * readPreference (MongoDB\Driver\ReadPreference): The default read
5762 * preference to use for collection operations. Defaults to the Manager's
5863 * read preference.
@@ -77,6 +82,10 @@ public function __construct(Manager $manager, $namespace, array $options = [])
7782 $ this ->databaseName = $ parts [0 ];
7883 $ this ->collectionName = $ parts [1 ];
7984
85+ if (isset ($ options ['readConcern ' ]) && ! $ options ['readConcern ' ] instanceof ReadConcern) {
86+ throw new InvalidArgumentTypeException ('"readConcern" option ' , $ options ['readConcern ' ], 'MongoDB\Driver\ReadConcern ' );
87+ }
88+
8089 if (isset ($ options ['readPreference ' ]) && ! $ options ['readPreference ' ] instanceof ReadPreference) {
8190 throw new InvalidArgumentTypeException ('"readPreference" option ' , $ options ['readPreference ' ], 'MongoDB\Driver\ReadPreference ' );
8291 }
@@ -86,6 +95,7 @@ public function __construct(Manager $manager, $namespace, array $options = [])
8695 }
8796
8897 $ this ->manager = $ manager ;
98+ $ this ->readConcern = isset ($ options ['readConcern ' ]) ? $ options ['readConcern ' ] : $ this ->manager ->getReadConcern ();
8999 $ this ->readPreference = isset ($ options ['readPreference ' ]) ? $ options ['readPreference ' ] : $ this ->manager ->getReadPreference ();
90100 $ this ->writeConcern = isset ($ options ['writeConcern ' ]) ? $ options ['writeConcern ' ] : $ this ->manager ->getWriteConcern ();
91101 }
@@ -102,6 +112,7 @@ public function __debugInfo()
102112 'collectionName ' => $ this ->collectionName ,
103113 'databaseName ' => $ this ->databaseName ,
104114 'manager ' => $ this ->manager ,
115+ 'readConcern ' => $ this ->readConcern ,
105116 'readPreference ' => $ this ->readPreference ,
106117 'writeConcern ' => $ this ->writeConcern ,
107118 ];
@@ -132,11 +143,20 @@ public function __toString()
132143 */
133144 public function aggregate (array $ pipeline , array $ options = [])
134145 {
146+ $ hasOutStage = \MongoDB \is_last_pipeline_operator_out ($ pipeline );
147+
148+ /* A "majority" read concern is not compatible with the $out stage, so
149+ * avoid providing the Collection's read concern if it would conflict.
150+ */
151+ if ( ! isset ($ options ['readConcern ' ]) && ! ($ hasOutStage && $ this ->readConcern ->getLevel () === ReadConcern::MAJORITY )) {
152+ $ options ['readConcern ' ] = $ this ->readConcern ;
153+ }
154+
135155 if ( ! isset ($ options ['readPreference ' ])) {
136156 $ options ['readPreference ' ] = $ this ->readPreference ;
137157 }
138158
139- if (\ MongoDB \is_last_pipeline_operator_out ( $ pipeline ) ) {
159+ if ($ hasOutStage ) {
140160 $ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
141161 }
142162
@@ -176,6 +196,10 @@ public function bulkWrite(array $operations, array $options = [])
176196 */
177197 public function count ($ filter = [], array $ options = [])
178198 {
199+ if ( ! isset ($ options ['readConcern ' ])) {
200+ $ options ['readConcern ' ] = $ this ->readConcern ;
201+ }
202+
179203 if ( ! isset ($ options ['readPreference ' ])) {
180204 $ options ['readPreference ' ] = $ this ->readPreference ;
181205 }
@@ -284,6 +308,10 @@ public function deleteOne($filter, array $options = [])
284308 */
285309 public function distinct ($ fieldName , $ filter = [], array $ options = [])
286310 {
311+ if ( ! isset ($ options ['readConcern ' ])) {
312+ $ options ['readConcern ' ] = $ this ->readConcern ;
313+ }
314+
287315 if ( ! isset ($ options ['readPreference ' ])) {
288316 $ options ['readPreference ' ] = $ this ->readPreference ;
289317 }
@@ -352,6 +380,10 @@ public function dropIndexes()
352380 */
353381 public function find ($ filter = [], array $ options = [])
354382 {
383+ if ( ! isset ($ options ['readConcern ' ])) {
384+ $ options ['readConcern ' ] = $ this ->readConcern ;
385+ }
386+
355387 if ( ! isset ($ options ['readPreference ' ])) {
356388 $ options ['readPreference ' ] = $ this ->readPreference ;
357389 }
@@ -373,6 +405,10 @@ public function find($filter = [], array $options = [])
373405 */
374406 public function findOne ($ filter = [], array $ options = [])
375407 {
408+ if ( ! isset ($ options ['readConcern ' ])) {
409+ $ options ['readConcern ' ] = $ this ->readConcern ;
410+ }
411+
376412 if ( ! isset ($ options ['readPreference ' ])) {
377413 $ options ['readPreference ' ] = $ this ->readPreference ;
378414 }
@@ -621,6 +657,10 @@ public function updateOne($filter, $update, array $options = [])
621657 *
622658 * Supported options:
623659 *
660+ * * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
661+ * use for collection operations. Defaults to this Collection's read
662+ * concern.
663+ *
624664 * * readPreference (MongoDB\Driver\ReadPreference): The default read
625665 * preference to use for collection operations. Defaults to this
626666 * Collection's read preference.
@@ -634,6 +674,10 @@ public function updateOne($filter, $update, array $options = [])
634674 */
635675 public function withOptions (array $ options = [])
636676 {
677+ if ( ! isset ($ options ['readConcern ' ])) {
678+ $ options ['readConcern ' ] = $ this ->readConcern ;
679+ }
680+
637681 if ( ! isset ($ options ['readPreference ' ])) {
638682 $ options ['readPreference ' ] = $ this ->readPreference ;
639683 }
0 commit comments