Skip to content

Commit 6081401

Browse files
committed
Updated FindOperation, Options and Iterables
Modifiers have now been deprecated and made first class options. JAVA-2427
1 parent 2b81f0b commit 6081401

File tree

13 files changed

+1338
-253
lines changed

13 files changed

+1338
-253
lines changed

driver-async/src/main/com/mongodb/async/client/FindIterable.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public interface FindIterable<T> extends MongoIterable<T> {
9292
* @param modifiers the query modifiers to apply, which may be null.
9393
* @return this
9494
* @mongodb.driver.manual reference/operator/query-modifier/ Query Modifiers
95+
* @deprecated use the individual setters instead
9596
*/
97+
@Deprecated
9698
FindIterable<T> modifiers(Bson modifiers);
9799

98100
/**
@@ -165,4 +167,80 @@ public interface FindIterable<T> extends MongoIterable<T> {
165167
* @mongodb.server.release 3.4
166168
*/
167169
FindIterable<T> collation(Collation collation);
170+
171+
/**
172+
* Sets the comment to the query. A null value means no comment is set.
173+
*
174+
* @param comment the comment
175+
* @return this
176+
* @since 3.5
177+
*/
178+
FindIterable<T> comment(String comment);
179+
180+
/**
181+
* Sets the hint for which index to use. A null value means no hint is set.
182+
*
183+
* @param hint the hint
184+
* @return this
185+
* @since 3.5
186+
*/
187+
FindIterable<T> hint(Bson hint);
188+
189+
/**
190+
* Sets the exclusive upper bound for a specific index. A null value means no max is set.
191+
*
192+
* @param max the max
193+
* @return this
194+
* @since 3.5
195+
*/
196+
FindIterable<T> max(Bson max);
197+
198+
/**
199+
* Sets the minimum inclusive lower bound for a specific index. A null value means no max is set.
200+
*
201+
* @param min the min
202+
* @return this
203+
* @since 3.5
204+
*/
205+
FindIterable<T> min(Bson min);
206+
207+
/**
208+
* Sets the maximum number of documents or index keys to scan when executing the query.
209+
*
210+
* A zero value or less will be ignored, and indicates that the driver should respect the server's default value.
211+
*
212+
* @param maxScan the maxScan
213+
* @return this
214+
* @since 3.5
215+
*/
216+
FindIterable<T> maxScan(long maxScan);
217+
218+
/**
219+
* Sets the returnKey. If true the find operation will return only the index keys in the resulting documents.
220+
*
221+
* @param returnKey the returnKey
222+
* @return this
223+
* @since 3.5
224+
*/
225+
FindIterable<T> returnKey(boolean returnKey);
226+
227+
/**
228+
* Sets the showRecordId. Set to true to add a field {@code $recordId} to the returned documents.
229+
*
230+
* @param showRecordId the showRecordId
231+
* @return this
232+
* @since 3.5
233+
*/
234+
FindIterable<T> showRecordId(boolean showRecordId);
235+
236+
/**
237+
* Sets the snapshot.
238+
*
239+
* If true it prevents the cursor from returning a document more than once because of an intervening write operation.
240+
*
241+
* @param snapshot the snapshot
242+
* @return this
243+
* @since 3.5
244+
*/
245+
FindIterable<T> snapshot(boolean snapshot);
168246
}

driver-async/src/main/com/mongodb/async/client/FindIterableImpl.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import static com.mongodb.assertions.Assertions.notNull;
3939
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4040

41+
42+
@SuppressWarnings("deprecation")
4143
class FindIterableImpl<TDocument, TResult> implements FindIterable<TResult> {
4244
private final MongoNamespace namespace;
4345
private final Class<TDocument> documentClass;
@@ -149,6 +151,54 @@ public FindIterable<TResult> cursorType(final CursorType cursorType) {
149151
return this;
150152
}
151153

154+
@Override
155+
public FindIterable<TResult> comment(final String comment) {
156+
findOptions.comment(comment);
157+
return this;
158+
}
159+
160+
@Override
161+
public FindIterable<TResult> hint(final Bson hint) {
162+
findOptions.hint(hint);
163+
return this;
164+
}
165+
166+
@Override
167+
public FindIterable<TResult> max(final Bson max) {
168+
findOptions.max(max);
169+
return this;
170+
}
171+
172+
@Override
173+
public FindIterable<TResult> min(final Bson min) {
174+
findOptions.min(min);
175+
return this;
176+
}
177+
178+
@Override
179+
public FindIterable<TResult> maxScan(final long maxScan) {
180+
findOptions.maxScan(maxScan);
181+
return this;
182+
}
183+
184+
@Override
185+
public FindIterable<TResult> returnKey(final boolean returnKey) {
186+
findOptions.returnKey(returnKey);
187+
return this;
188+
}
189+
190+
@Override
191+
public FindIterable<TResult> showRecordId(final boolean showRecordId) {
192+
findOptions.showRecordId(showRecordId);
193+
return this;
194+
}
195+
196+
@Override
197+
public FindIterable<TResult> snapshot(final boolean snapshot) {
198+
findOptions.snapshot(snapshot);
199+
return this;
200+
}
201+
152202
@Override
153203
public void first(final SingleResultCallback<TResult> callback) {
154204
notNull("callback", callback);
@@ -205,7 +255,15 @@ private FindOperation<TResult> createQueryOperation() {
205255
.partial(findOptions.isPartial())
206256
.slaveOk(readPreference.isSlaveOk())
207257
.readConcern(readConcern)
208-
.collation(findOptions.getCollation());
258+
.collation(findOptions.getCollation())
259+
.comment(findOptions.getComment())
260+
.hint(toBsonDocument(findOptions.getHint()))
261+
.min(toBsonDocument(findOptions.getMin()))
262+
.max(toBsonDocument(findOptions.getMax()))
263+
.maxScan(findOptions.getMaxScan())
264+
.returnKey(findOptions.isReturnKey())
265+
.showRecordId(findOptions.isShowRecordId())
266+
.snapshot(findOptions.isSnapshot());
209267
}
210268

211269
private BsonDocument toBsonDocument(final Bson document) {

driver-async/src/test/unit/com/mongodb/async/client/FindIterableSpecification.groovy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class FindIterableSpecification extends Specification {
5252
def readConcern = ReadConcern.DEFAULT
5353
def collation = Collation.builder().locale('en').build()
5454

55+
@SuppressWarnings('deprecation')
5556
def 'should build the expected findOperation'() {
5657
given:
5758
def cursor = Stub(AsyncBatchCursor) {
@@ -73,6 +74,14 @@ class FindIterableSpecification extends Specification {
7374
.noCursorTimeout(false)
7475
.partial(false)
7576
.collation(null)
77+
.comment('my comment')
78+
.hint(new Document('hint', 1))
79+
.min(new Document('min', 1))
80+
.max(new Document('max', 1))
81+
.maxScan(42L)
82+
.returnKey(false)
83+
.showRecordId(false)
84+
.snapshot(false)
7685
def findIterable = new FindIterableImpl(namespace, Document, Document, codecRegistry, readPreference, readConcern, executor,
7786
new Document('filter', 1), findOptions)
7887

@@ -95,6 +104,14 @@ class FindIterableSpecification extends Specification {
95104
.skip(10)
96105
.cursorType(CursorType.NonTailable)
97106
.slaveOk(true)
107+
.comment('my comment')
108+
.hint(new BsonDocument('hint', new BsonInt32(1)))
109+
.min(new BsonDocument('min', new BsonInt32(1)))
110+
.max(new BsonDocument('max', new BsonInt32(1)))
111+
.maxScan(42L)
112+
.returnKey(false)
113+
.showRecordId(false)
114+
.snapshot(false)
98115
)
99116
readPreference == secondary()
100117

@@ -113,6 +130,14 @@ class FindIterableSpecification extends Specification {
113130
.noCursorTimeout(true)
114131
.partial(true)
115132
.collation(collation)
133+
.comment('alt comment')
134+
.hint(new Document('hint', 2))
135+
.min(new Document('min', 2))
136+
.max(new Document('max', 2))
137+
.maxScan(88L)
138+
.returnKey(true)
139+
.showRecordId(true)
140+
.snapshot(true)
116141
.into([]) { result, t -> }
117142

118143
operation = executor.getReadOperation() as FindOperation<Document>
@@ -134,6 +159,14 @@ class FindIterableSpecification extends Specification {
134159
.partial(true)
135160
.slaveOk(true)
136161
.collation(collation)
162+
.comment('alt comment')
163+
.hint(new BsonDocument('hint', new BsonInt32(2)))
164+
.min(new BsonDocument('min', new BsonInt32(2)))
165+
.max(new BsonDocument('max', new BsonInt32(2)))
166+
.maxScan(88L)
167+
.returnKey(true)
168+
.showRecordId(true)
169+
.snapshot(true)
137170
)
138171
}
139172

0 commit comments

Comments
 (0)