5
5
use MongoDB \Driver \Command ;
6
6
use MongoDB \Driver \Cursor ;
7
7
use MongoDB \Driver \Manager ;
8
+ use MongoDB \Driver \ReadConcern ;
8
9
use MongoDB \Driver \ReadPreference ;
9
10
use MongoDB \Driver \Server ;
10
11
use MongoDB \Driver \WriteConcern ;
@@ -42,6 +43,7 @@ class Collection
42
43
private $ collectionName ;
43
44
private $ databaseName ;
44
45
private $ manager ;
46
+ private $ readConcern ;
45
47
private $ readPreference ;
46
48
private $ writeConcern ;
47
49
@@ -53,6 +55,9 @@ class Collection
53
55
*
54
56
* Supported options:
55
57
*
58
+ * * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
59
+ * use for collection operations. Defaults to the Manager's read concern.
60
+ *
56
61
* * readPreference (MongoDB\Driver\ReadPreference): The default read
57
62
* preference to use for collection operations. Defaults to the Manager's
58
63
* read preference.
@@ -77,6 +82,10 @@ public function __construct(Manager $manager, $namespace, array $options = [])
77
82
$ this ->databaseName = $ parts [0 ];
78
83
$ this ->collectionName = $ parts [1 ];
79
84
85
+ if (isset ($ options ['readConcern ' ]) && ! $ options ['readConcern ' ] instanceof ReadConcern) {
86
+ throw new InvalidArgumentTypeException ('"readConcern" option ' , $ options ['readConcern ' ], 'MongoDB\Driver\ReadConcern ' );
87
+ }
88
+
80
89
if (isset ($ options ['readPreference ' ]) && ! $ options ['readPreference ' ] instanceof ReadPreference) {
81
90
throw new InvalidArgumentTypeException ('"readPreference" option ' , $ options ['readPreference ' ], 'MongoDB\Driver\ReadPreference ' );
82
91
}
@@ -86,6 +95,7 @@ public function __construct(Manager $manager, $namespace, array $options = [])
86
95
}
87
96
88
97
$ this ->manager = $ manager ;
98
+ $ this ->readConcern = isset ($ options ['readConcern ' ]) ? $ options ['readConcern ' ] : $ this ->manager ->getReadConcern ();
89
99
$ this ->readPreference = isset ($ options ['readPreference ' ]) ? $ options ['readPreference ' ] : $ this ->manager ->getReadPreference ();
90
100
$ this ->writeConcern = isset ($ options ['writeConcern ' ]) ? $ options ['writeConcern ' ] : $ this ->manager ->getWriteConcern ();
91
101
}
@@ -102,6 +112,7 @@ public function __debugInfo()
102
112
'collectionName ' => $ this ->collectionName ,
103
113
'databaseName ' => $ this ->databaseName ,
104
114
'manager ' => $ this ->manager ,
115
+ 'readConcern ' => $ this ->readConcern ,
105
116
'readPreference ' => $ this ->readPreference ,
106
117
'writeConcern ' => $ this ->writeConcern ,
107
118
];
@@ -132,11 +143,20 @@ public function __toString()
132
143
*/
133
144
public function aggregate (array $ pipeline , array $ options = [])
134
145
{
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
+
135
155
if ( ! isset ($ options ['readPreference ' ])) {
136
156
$ options ['readPreference ' ] = $ this ->readPreference ;
137
157
}
138
158
139
- if (\ MongoDB \is_last_pipeline_operator_out ( $ pipeline ) ) {
159
+ if ($ hasOutStage ) {
140
160
$ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
141
161
}
142
162
@@ -176,6 +196,10 @@ public function bulkWrite(array $operations, array $options = [])
176
196
*/
177
197
public function count ($ filter = [], array $ options = [])
178
198
{
199
+ if ( ! isset ($ options ['readConcern ' ])) {
200
+ $ options ['readConcern ' ] = $ this ->readConcern ;
201
+ }
202
+
179
203
if ( ! isset ($ options ['readPreference ' ])) {
180
204
$ options ['readPreference ' ] = $ this ->readPreference ;
181
205
}
@@ -284,6 +308,10 @@ public function deleteOne($filter, array $options = [])
284
308
*/
285
309
public function distinct ($ fieldName , $ filter = [], array $ options = [])
286
310
{
311
+ if ( ! isset ($ options ['readConcern ' ])) {
312
+ $ options ['readConcern ' ] = $ this ->readConcern ;
313
+ }
314
+
287
315
if ( ! isset ($ options ['readPreference ' ])) {
288
316
$ options ['readPreference ' ] = $ this ->readPreference ;
289
317
}
@@ -352,6 +380,10 @@ public function dropIndexes()
352
380
*/
353
381
public function find ($ filter = [], array $ options = [])
354
382
{
383
+ if ( ! isset ($ options ['readConcern ' ])) {
384
+ $ options ['readConcern ' ] = $ this ->readConcern ;
385
+ }
386
+
355
387
if ( ! isset ($ options ['readPreference ' ])) {
356
388
$ options ['readPreference ' ] = $ this ->readPreference ;
357
389
}
@@ -373,6 +405,10 @@ public function find($filter = [], array $options = [])
373
405
*/
374
406
public function findOne ($ filter = [], array $ options = [])
375
407
{
408
+ if ( ! isset ($ options ['readConcern ' ])) {
409
+ $ options ['readConcern ' ] = $ this ->readConcern ;
410
+ }
411
+
376
412
if ( ! isset ($ options ['readPreference ' ])) {
377
413
$ options ['readPreference ' ] = $ this ->readPreference ;
378
414
}
@@ -621,6 +657,10 @@ public function updateOne($filter, $update, array $options = [])
621
657
*
622
658
* Supported options:
623
659
*
660
+ * * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
661
+ * use for collection operations. Defaults to this Collection's read
662
+ * concern.
663
+ *
624
664
* * readPreference (MongoDB\Driver\ReadPreference): The default read
625
665
* preference to use for collection operations. Defaults to this
626
666
* Collection's read preference.
@@ -634,6 +674,10 @@ public function updateOne($filter, $update, array $options = [])
634
674
*/
635
675
public function withOptions (array $ options = [])
636
676
{
677
+ if ( ! isset ($ options ['readConcern ' ])) {
678
+ $ options ['readConcern ' ] = $ this ->readConcern ;
679
+ }
680
+
637
681
if ( ! isset ($ options ['readPreference ' ])) {
638
682
$ options ['readPreference ' ] = $ this ->readPreference ;
639
683
}
0 commit comments