@@ -121,6 +121,7 @@ public class DBCollection {
121
121
private final Bytes .OptionHolder optionHolder ;
122
122
private volatile ReadPreference readPreference ;
123
123
private volatile WriteConcern writeConcern ;
124
+ private volatile ReadConcern readConcern ;
124
125
private List <DBObject > hintFields ;
125
126
private DBEncoderFactory encoderFactory ;
126
127
private DBDecoderFactory decoderFactory ;
@@ -743,7 +744,7 @@ public DBObject findOne(final DBObject query, final DBObject projection, final R
743
744
*/
744
745
public DBObject findOne (final DBObject query , final DBObject projection , final DBObject sort ,
745
746
final ReadPreference readPreference ) {
746
- return findOne (query , projection , sort , readPreference , 0 , MILLISECONDS );
747
+ return findOne (query , projection , sort , readPreference , getReadConcern (), 0 , MILLISECONDS );
747
748
}
748
749
749
750
/**
@@ -753,16 +754,19 @@ public DBObject findOne(final DBObject query, final DBObject projection, final D
753
754
* @param projection specifies which projection MongoDB will return from the documents in the result set.
754
755
* @param sort A document whose fields specify the attributes on which to sort the result set.
755
756
* @param readPreference {@code ReadPreference} to be used for this operation
757
+ * @param readConcern {@code ReadConcern} to be used for this operation
756
758
* @param maxTime the maximum time that the server will allow this operation to execute before killing it
757
759
* @param maxTimeUnit the unit that maxTime is specified in
758
760
* @return A document that satisfies the query specified as the argument to this method.
759
761
* @mongodb.driver.manual tutorial/query-documents/ Querying
760
762
* @since 2.12.0
761
763
*/
762
764
DBObject findOne (final DBObject query , final DBObject projection , final DBObject sort ,
763
- final ReadPreference readPreference , final long maxTime , final TimeUnit maxTimeUnit ) {
765
+ final ReadPreference readPreference , final ReadConcern readConcern ,
766
+ final long maxTime , final TimeUnit maxTimeUnit ) {
764
767
FindOperation <DBObject > operation = new FindOperation <DBObject >(getNamespace (),
765
768
objectCodec )
769
+ .readConcern (readConcern )
766
770
.projection (wrapAllowNull (projection ))
767
771
.sort (wrapAllowNull (sort ))
768
772
.limit (-1 )
@@ -924,16 +928,18 @@ public long getCount(final DBObject query, final DBObject projection, final long
924
928
*/
925
929
public long getCount (final DBObject query , final DBObject projection , final long limit , final long skip ,
926
930
final ReadPreference readPreference ) {
927
- return getCount (query , projection , limit , skip , readPreference , 0 , MILLISECONDS );
931
+ return getCount (query , limit , skip , readPreference , getReadConcern () , 0 , MILLISECONDS );
928
932
}
929
933
930
- long getCount (final DBObject query , final DBObject projection , final long limit , final long skip ,
931
- final ReadPreference readPreference , final long maxTime , final TimeUnit maxTimeUnit ) {
932
- return getCount (query , projection , limit , skip , readPreference , maxTime , maxTimeUnit , null );
934
+ long getCount (final DBObject query , final long limit , final long skip ,
935
+ final ReadPreference readPreference , final ReadConcern readConcern ,
936
+ final long maxTime , final TimeUnit maxTimeUnit ) {
937
+ return getCount (query , limit , skip , readPreference , readConcern , maxTime , maxTimeUnit , null );
933
938
}
934
939
935
- long getCount (final DBObject query , final DBObject projection , final long limit , final long skip ,
936
- final ReadPreference readPreference , final long maxTime , final TimeUnit maxTimeUnit ,
940
+ long getCount (final DBObject query , final long limit , final long skip ,
941
+ final ReadPreference readPreference , final ReadConcern readConcern ,
942
+ final long maxTime , final TimeUnit maxTimeUnit ,
937
943
final BsonValue hint ) {
938
944
939
945
if (limit > Integer .MAX_VALUE ) {
@@ -945,6 +951,7 @@ long getCount(final DBObject query, final DBObject projection, final long limit,
945
951
}
946
952
947
953
CountOperation operation = new CountOperation (getNamespace ())
954
+ .readConcern (readConcern )
948
955
.hint (hint )
949
956
.skip (skip )
950
957
.limit (limit )
@@ -1105,7 +1112,9 @@ public List distinct(final String fieldName, final DBObject query) {
1105
1112
@ SuppressWarnings ("unchecked" )
1106
1113
public List distinct (final String fieldName , final DBObject query , final ReadPreference readPreference ) {
1107
1114
return new OperationIterable <BsonValue >(new DistinctOperation <BsonValue >(getNamespace (), fieldName ,
1108
- new BsonValueCodec ()).filter (wrap (query )),
1115
+ new BsonValueCodec ())
1116
+ .readConcern (getReadConcern ())
1117
+ .filter (wrap (query )),
1109
1118
readPreference , executor ).map (new Function <BsonValue , Object >() {
1110
1119
@ Override
1111
1120
public Object apply (final BsonValue bsonValue ) {
@@ -1186,7 +1195,7 @@ public MapReduceOutput mapReduce(final MapReduceCommand command) {
1186
1195
new BsonJavaScript (command .getMap ()),
1187
1196
new BsonJavaScript (command .getReduce ()),
1188
1197
getDefaultDBObjectCodec ());
1189
-
1198
+ operation . readConcern ( getReadConcern ());
1190
1199
operation .filter (wrapAllowNull (command .getQuery ()));
1191
1200
operation .limit (command .getLimit ());
1192
1201
operation .maxTime (command .getMaxTime (MILLISECONDS ), MILLISECONDS );
@@ -1360,6 +1369,7 @@ private Cursor aggregate(final List<? extends DBObject> pipeline, final Aggregat
1360
1369
}
1361
1370
} else {
1362
1371
AggregateOperation <DBObject > operation = new AggregateOperation <DBObject >(getNamespace (), stages , objectCodec )
1372
+ .readConcern (getReadConcern ())
1363
1373
.maxTime (options .getMaxTime (MILLISECONDS ), MILLISECONDS )
1364
1374
.allowDiskUse (options .getAllowDiskUse ())
1365
1375
.batchSize (options .getBatchSize ())
@@ -1416,6 +1426,7 @@ public List<Cursor> parallelScan(final ParallelScanOptions options) {
1416
1426
ParallelCollectionScanOperation <DBObject > operation = new ParallelCollectionScanOperation <DBObject >(getNamespace (),
1417
1427
options .getNumCursors (),
1418
1428
objectCodec )
1429
+ .readConcern (getReadConcern ())
1419
1430
.batchSize (options .getBatchSize ());
1420
1431
List <BatchCursor <DBObject >> mongoCursors = executor .execute (operation ,
1421
1432
options .getReadPreference () != null ? options .getReadPreference ()
@@ -1871,6 +1882,32 @@ public void setReadPreference(final ReadPreference preference) {
1871
1882
this .readPreference = preference ;
1872
1883
}
1873
1884
1885
+ /**
1886
+ * Sets the read concern for this collection.
1887
+ *
1888
+ * @param readConcern the read concern to use for this collection
1889
+ * @since 3.2
1890
+ * @mongodb.server.release 3.2
1891
+ */
1892
+ void setReadConcern (final ReadConcern readConcern ) {
1893
+ this .readConcern = readConcern ;
1894
+ }
1895
+
1896
+ /**
1897
+ * Get the read concern for this collection.
1898
+ *
1899
+ * @return the {@link com.mongodb.ReadConcern}
1900
+ * @since 3.2
1901
+ * @mongodb.server.release 3.2
1902
+ */
1903
+ ReadConcern getReadConcern () {
1904
+ if (readConcern != null ) {
1905
+ return readConcern ;
1906
+ }
1907
+ return database .getReadConcern ();
1908
+ }
1909
+
1910
+
1874
1911
/**
1875
1912
* Makes this query ok to run on a slave node
1876
1913
*
0 commit comments