Skip to content

Commit 56ac839

Browse files
committed
DATAMONGO-2319 - Deprecate Query.withHint(String) and introduce withHint(Document).
The $hint operator is deprecated since MongoDB 3.2 so we're now deprecating Query.withHint() accepting a String as the String is expected to be a valid document. Therefore, we're introducing withHint(Document) to accept a type-safe representation of query hints.
1 parent 945d3b0 commit 56ac839

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,32 @@ public Query limit(int limit) {
142142
}
143143

144144
/**
145-
* Configures the query to use the given hint when being executed.
145+
* Configures the query to use the given hint when being executed. {@code hint} is parsed as {@link Document}.
146146
*
147-
* @param name must not be {@literal null} or empty.
147+
* @param hint must not be {@literal null} or empty.
148148
* @return
149+
* @see Document#parse(String)
150+
* @deprecated since 2.2, use {@link #withHint(Document)}
149151
*/
150-
public Query withHint(String name) {
151-
Assert.hasText(name, "Hint must not be empty or null!");
152-
this.hint = name;
152+
@Deprecated
153+
public Query withHint(String hint) {
154+
155+
Assert.hasText(hint, "Hint must not be empty or null!");
156+
this.hint = hint;
157+
return this;
158+
}
159+
160+
/**
161+
* Configures the query to use the given {@link Document hint} when being executed.
162+
*
163+
* @param hint must not be {@literal null}.
164+
* @return
165+
* @since 2.2
166+
*/
167+
public Query withHint(Document hint) {
168+
169+
Assert.notNull(hint, "Hint must not be null!");
170+
this.hint = hint.toJson();
153171
return this;
154172
}
155173

@@ -284,8 +302,10 @@ public int getLimit() {
284302

285303
/**
286304
* @return
305+
* @deprecated since 2.2. Return type to be changed to {@link Document}.
287306
*/
288307
@Nullable
308+
@Deprecated
289309
public String getHint() {
290310
return hint;
291311
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryCursorPreparerUnitTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ public void appliesHintsCorrectly() {
7373
verify(cursor).hint(new Document("age", 1));
7474
}
7575

76+
@Test // DATAMONGO-2319
77+
public void appliesDocumentHintsCorrectly() {
78+
79+
Query query = query(where("foo").is("bar")).withHint(Document.parse("{ age: 1 }"));
80+
prepare(query);
81+
82+
verify(cursor).hint(new Document("age", 1));
83+
}
84+
7685
@Test // DATAMONGO-957
7786
public void doesNotApplyMetaWhenEmpty() {
7887

0 commit comments

Comments
 (0)