Skip to content

Commit 02a7f06

Browse files
committed
Deprecate DBCollectionFindOptions#getModifiers
Replace with per-modifier properties: * comment * hint * min * max * returnKey * showRecordId JAVA-3022
1 parent bf76d5d commit 02a7f06

File tree

4 files changed

+224
-7
lines changed

4 files changed

+224
-7
lines changed

driver-legacy/src/main/com/mongodb/DBCursor.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public DBCursor addSpecial(@Nullable final String name, @Nullable final Object v
349349
* @since 2.12
350350
*/
351351
public DBCursor comment(final String comment) {
352-
findOptions.getModifiers().put("$comment", comment);
352+
findOptions.comment(comment);
353353
return this;
354354
}
355355

@@ -378,7 +378,7 @@ public DBCursor maxScan(final int max) {
378378
* @since 2.12
379379
*/
380380
public DBCursor max(final DBObject max) {
381-
findOptions.getModifiers().put("$max", max);
381+
findOptions.max(max);
382382
return this;
383383
}
384384

@@ -391,7 +391,7 @@ public DBCursor max(final DBObject max) {
391391
* @since 2.12
392392
*/
393393
public DBCursor min(final DBObject min) {
394-
findOptions.getModifiers().put("$min", min);
394+
findOptions.min(min);
395395
return this;
396396
}
397397

@@ -403,7 +403,7 @@ public DBCursor min(final DBObject min) {
403403
* @since 2.12
404404
*/
405405
public DBCursor returnKey() {
406-
findOptions.getModifiers().put("$returnKey", true);
406+
findOptions.returnKey(true);
407407
return this;
408408
}
409409

@@ -428,7 +428,7 @@ public DBCursor showDiskLoc() {
428428
* @mongodb.driver.manual reference/operator/meta/hint/ $hint
429429
*/
430430
public DBCursor hint(final DBObject indexKeys) {
431-
findOptions.getModifiers().put("$hint", indexKeys);
431+
findOptions.hint(indexKeys);
432432
return this;
433433
}
434434

@@ -559,7 +559,13 @@ private FindOperation<DBObject> getQueryOperation(final Decoder<DBObject> decode
559559
.modifiers(collection.wrapAllowNull(findOptions.getModifiers()))
560560
.projection(collection.wrapAllowNull(findOptions.getProjection()))
561561
.sort(collection.wrapAllowNull(findOptions.getSort()))
562-
.collation(findOptions.getCollation());
562+
.collation(findOptions.getCollation())
563+
.comment(findOptions.getComment())
564+
.hint(collection.wrapAllowNull(findOptions.getHint()))
565+
.min(collection.wrapAllowNull(findOptions.getMin()))
566+
.max(collection.wrapAllowNull(findOptions.getMax()))
567+
.returnKey(findOptions.isReturnKey())
568+
.showRecordId(findOptions.isShowRecordId());
563569

564570
if ((this.options & Bytes.QUERYOPTION_TAILABLE) != 0) {
565571
if ((this.options & Bytes.QUERYOPTION_AWAITDATA) != 0) {
@@ -1080,7 +1086,7 @@ private DBCollectionCountOptions getDbCollectionCountOptions() {
10801086
.collation(getCollation())
10811087
.maxTime(findOptions.getMaxTime(MILLISECONDS), MILLISECONDS);
10821088

1083-
Object hint = findOptions.getModifiers().get("$hint");
1089+
Object hint = findOptions.getHint() != null ? findOptions.getHint() : findOptions.getModifiers().get("$hint");
10841090
if (hint != null) {
10851091
if (hint instanceof String) {
10861092
countOptions.hintString((String) hint);

driver-legacy/src/main/com/mongodb/client/model/DBCollectionFindOptions.java

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public final class DBCollectionFindOptions {
5151
private ReadPreference readPreference;
5252
private ReadConcern readConcern;
5353
private Collation collation;
54+
private String comment;
55+
private DBObject hint;
56+
private DBObject max;
57+
private DBObject min;
58+
private boolean returnKey;
59+
private boolean showRecordId;
5460

5561
/**
5662
* Construct a new instance
@@ -80,6 +86,12 @@ public DBCollectionFindOptions copy() {
8086
copiedOptions.readPreference(readPreference);
8187
copiedOptions.readConcern(readConcern);
8288
copiedOptions.collation(collation);
89+
copiedOptions.comment(comment);
90+
copiedOptions.hint(hint);
91+
copiedOptions.max(max);
92+
copiedOptions.min(min);
93+
copiedOptions.returnKey(returnKey);
94+
copiedOptions.showRecordId(showRecordId);
8395
return copiedOptions;
8496
}
8597

@@ -435,4 +447,145 @@ public DBCollectionFindOptions collation(@Nullable final Collation collation) {
435447
this.collation = collation;
436448
return this;
437449
}
450+
451+
/**
452+
* Returns the comment to send with the query. The default is not to include a comment with the query.
453+
*
454+
* @return the comment
455+
* @since 3.9
456+
*/
457+
@Nullable
458+
public String getComment() {
459+
return comment;
460+
}
461+
462+
/**
463+
* Sets the comment to the query. A null value means no comment is set.
464+
*
465+
* @param comment the comment
466+
* @return this
467+
* @since 3.9
468+
*/
469+
public DBCollectionFindOptions comment(@Nullable final String comment) {
470+
this.comment = comment;
471+
return this;
472+
}
473+
474+
/**
475+
* Returns the hint for which index to use. The default is not to set a hint.
476+
*
477+
* @return the hint
478+
* @since 3.9
479+
*/
480+
@Nullable
481+
public DBObject getHint() {
482+
return hint;
483+
}
484+
485+
/**
486+
* Sets the hint for which index to use. A null value means no hint is set.
487+
*
488+
* @param hint the hint
489+
* @return this
490+
* @since 3.9
491+
*/
492+
public DBCollectionFindOptions hint(@Nullable final DBObject hint) {
493+
this.hint = hint;
494+
return this;
495+
}
496+
497+
/**
498+
* Returns the exclusive upper bound for a specific index. By default there is no max bound.
499+
*
500+
* @return the max
501+
* @since 3.9
502+
*/
503+
@Nullable
504+
public DBObject getMax() {
505+
return max;
506+
}
507+
508+
/**
509+
* Sets the exclusive upper bound for a specific index. A null value means no max is set.
510+
*
511+
* @param max the max
512+
* @return this
513+
* @since 3.9
514+
*/
515+
public DBCollectionFindOptions max(@Nullable final DBObject max) {
516+
this.max = max;
517+
return this;
518+
}
519+
520+
/**
521+
* Returns the minimum inclusive lower bound for a specific index. By default there is no min bound.
522+
*
523+
* @return the min
524+
* @since 3.9
525+
*/
526+
@Nullable
527+
public DBObject getMin() {
528+
return min;
529+
}
530+
531+
/**
532+
* Sets the minimum inclusive lower bound for a specific index. A null value means no max is set.
533+
*
534+
* @param min the min
535+
* @return this
536+
* @since 3.9
537+
*/
538+
public DBCollectionFindOptions min(@Nullable final DBObject min) {
539+
this.min = min;
540+
return this;
541+
}
542+
543+
/**
544+
* Returns the returnKey. If true the find operation will return only the index keys in the resulting documents.
545+
*
546+
* Default value is false. If returnKey is true and the find command does not use an index, the returned documents will be empty.
547+
*
548+
* @return the returnKey
549+
* @since 3.9
550+
*/
551+
public boolean isReturnKey() {
552+
return returnKey;
553+
}
554+
555+
/**
556+
* Sets the returnKey. If true the find operation will return only the index keys in the resulting documents.
557+
*
558+
* @param returnKey the returnKey
559+
* @return this
560+
* @since 3.9
561+
*/
562+
public DBCollectionFindOptions returnKey(final boolean returnKey) {
563+
this.returnKey = returnKey;
564+
return this;
565+
}
566+
567+
/**
568+
* Returns the showRecordId.
569+
*
570+
* Determines whether to return the record identifier for each document. If true, adds a field $recordId to the returned documents.
571+
* The default is false.
572+
*
573+
* @return the showRecordId
574+
* @since 3.9
575+
*/
576+
public boolean isShowRecordId() {
577+
return showRecordId;
578+
}
579+
580+
/**
581+
* Sets the showRecordId. Set to true to add a field {@code $recordId} to the returned documents.
582+
*
583+
* @param showRecordId the showRecordId
584+
* @return this
585+
* @since 3.9
586+
*/
587+
public DBCollectionFindOptions showRecordId(final boolean showRecordId) {
588+
this.showRecordId = showRecordId;
589+
return this;
590+
}
438591
}

driver-legacy/src/test/unit/com/mongodb/DBCursorSpecification.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ class DBCursorSpecification extends Specification {
211211
def bsonModifiers = BsonDocument.parse(modifiers.toJson())
212212
def bsonProjection = BsonDocument.parse(projection.toJson())
213213
def bsonSort = BsonDocument.parse(sort.toJson())
214+
def comment = 'comment'
215+
def hint = BasicDBObject.parse('{x : 1}')
216+
def min = BasicDBObject.parse('{y : 1}')
217+
def max = BasicDBObject.parse('{y : 100}')
218+
def bsonHint = BsonDocument.parse(hint.toJson())
219+
def bsonMin = BsonDocument.parse(min.toJson())
220+
def bsonMax = BsonDocument.parse(max.toJson())
214221
def readConcern = ReadConcern.LOCAL
215222
def readPreference = ReadPreference.nearest()
216223
def findOptions = new DBCollectionFindOptions()
@@ -229,6 +236,13 @@ class DBCursorSpecification extends Specification {
229236
.readPreference(readPreference)
230237
.skip(1)
231238
.sort(sort)
239+
.comment(comment)
240+
.hint(hint)
241+
.max(max)
242+
.min(min)
243+
.returnKey(true)
244+
.showRecordId(true)
245+
232246
def cursor = new DBCursor(collection, filter, findOptions)
233247

234248
when:
@@ -250,6 +264,12 @@ class DBCursorSpecification extends Specification {
250264
.projection(bsonProjection)
251265
.skip(1)
252266
.sort(bsonSort)
267+
.comment(comment)
268+
.hint(bsonHint)
269+
.max(bsonMax)
270+
.min(bsonMin)
271+
.returnKey(true)
272+
.showRecordId(true)
253273
)
254274

255275
executor.getReadPreference() == findOptions.getReadPreference()

driver-legacy/src/test/unit/com/mongodb/client/model/DBCollectionFindOptionsSpecification.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class DBCollectionFindOptionsSpecification extends Specification {
4646
options.getReadPreference() == null
4747
options.getSkip() == 0
4848
options.getSort() == null
49+
options.getComment() == null
50+
options.getHint() == null
51+
options.getMax() == null
52+
options.getMin() == null
53+
!options.isReturnKey()
54+
!options.isShowRecordId()
4955
}
5056

5157
def 'should set and return the expected values'() {
@@ -57,6 +63,10 @@ class DBCollectionFindOptionsSpecification extends Specification {
5763
def modifiers = BasicDBObject.parse('{$comment: 1}')
5864
def readConcern = ReadConcern.LOCAL
5965
def readPreference = ReadPreference.nearest()
66+
def comment = 'comment'
67+
def hint = BasicDBObject.parse('{x : 1}')
68+
def min = BasicDBObject.parse('{y : 1}')
69+
def max = BasicDBObject.parse('{y : 100}')
6070

6171
when:
6272
def options = new DBCollectionFindOptions()
@@ -75,6 +85,12 @@ class DBCollectionFindOptionsSpecification extends Specification {
7585
.readPreference(readPreference)
7686
.skip(1)
7787
.sort(sort)
88+
.comment(comment)
89+
.hint(hint)
90+
.max(max)
91+
.min(min)
92+
.returnKey(true)
93+
.showRecordId(true)
7894

7995
then:
8096
options.getBatchSize() == 1
@@ -92,6 +108,12 @@ class DBCollectionFindOptionsSpecification extends Specification {
92108
options.isNoCursorTimeout()
93109
options.isOplogReplay()
94110
options.isPartial()
111+
options.getComment() == comment
112+
options.getHint() == hint
113+
options.getMax() == max
114+
options.getMin() == min
115+
options.isReturnKey()
116+
options.isShowRecordId()
95117
}
96118

97119
def 'it should copy and return the expected values'() {
@@ -103,6 +125,10 @@ class DBCollectionFindOptionsSpecification extends Specification {
103125
def modifiers = BasicDBObject.parse('{$comment: 1}')
104126
def readConcern = ReadConcern.LOCAL
105127
def readPreference = ReadPreference.nearest()
128+
def comment = 'comment'
129+
def hint = BasicDBObject.parse('{x : 1}')
130+
def min = BasicDBObject.parse('{y : 1}')
131+
def max = BasicDBObject.parse('{y : 100}')
106132

107133
when:
108134
def original = new DBCollectionFindOptions()
@@ -121,6 +147,12 @@ class DBCollectionFindOptionsSpecification extends Specification {
121147
.readPreference(readPreference)
122148
.skip(1)
123149
.sort(sort)
150+
.comment(comment)
151+
.hint(hint)
152+
.max(max)
153+
.min(min)
154+
.returnKey(true)
155+
.showRecordId(true)
124156

125157
def options = original.copy()
126158

@@ -142,6 +174,12 @@ class DBCollectionFindOptionsSpecification extends Specification {
142174
options.isNoCursorTimeout()
143175
options.isOplogReplay()
144176
options.isPartial()
177+
options.getComment() == comment
178+
options.getHint() == hint
179+
options.getMax() == max
180+
options.getMin() == min
181+
options.isReturnKey()
182+
options.isShowRecordId()
145183
}
146184

147185
}

0 commit comments

Comments
 (0)