Skip to content

Commit be9f3e9

Browse files
Add Criteria.eq(...).
The Criteria API now offers a dedicated eq method abstracting MongoDB $eq query operator. This allows users to create $not $eq operations instead of having to defer to the ne method. See: #4606
1 parent ec5eb4c commit be9f3e9

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ public Criteria and(String key) {
200200
@Contract("_ -> this")
201201
public Criteria is(@Nullable Object value) {
202202

203+
if (lastOperatorWasNot()) {
204+
return eq(value);
205+
}
206+
203207
if (!NOT_SET.equals(isValue)) {
204208
throw new InvalidMongoDbApiUsageException(
205209
"Multiple 'is' values declared; You need to use 'and' with multiple criteria");
206210
}
207211

208-
if (lastOperatorWasNot()) {
209-
throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead");
210-
}
211-
212212
this.isValue = value;
213213
return this;
214214
}
@@ -253,6 +253,21 @@ private boolean lastOperatorWasNot() {
253253
return !this.criteria.isEmpty() && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1]);
254254
}
255255

256+
/**
257+
* Creates a criterion using the {@literal $eq} operator.
258+
*
259+
* @param value can be {@literal null}.
260+
* @return this.
261+
* @see <a href="https://docs.mongodb.com/manual/reference/operator/query/eq/">MongoDB Query operator: $eq</a>
262+
* @since 5.0.4
263+
*/
264+
@Contract("_ -> this")
265+
public Criteria eq(Object value) {
266+
267+
criteria.put("$eq", value);
268+
return this;
269+
}
270+
256271
/**
257272
* Creates a criterion using the {@literal $ne} operator.
258273
*

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ void testSimpleCriteria() {
5353
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : \"Bubba\"}");
5454
}
5555

56+
@Test // GH-4606
57+
void testEqualsCriteria() {
58+
59+
Criteria c = new Criteria("name").eq("Bubba");
60+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : { \"$eq\" : \"Bubba\"} }");
61+
}
62+
5663
@Test // GH-4850
5764
void testCombiningSimpleCriteria() {
5865

@@ -70,6 +77,34 @@ void testCombiningSimpleCriteria() {
7077
assertThat(c.getCriteriaObject()).isEqualTo(expected);
7178
}
7279

80+
@Test // GH-4606
81+
void testCombiningSimpleAndNegatedEqCriteria() {
82+
83+
Document expected = Document.parse("{ name : { $eq : 123, $type : ['long'], $not : { $eq : 'foo' } } }");
84+
85+
Criteria c = Criteria.where("name") //
86+
.is(123) //
87+
.type(Type.INT_64)
88+
.not()
89+
.eq("foo");
90+
91+
assertThat(c.getCriteriaObject()).isEqualTo(expected);
92+
}
93+
94+
@Test // GH-4606
95+
void testCombiningSimpleAndNegatedIsCriteria() {
96+
97+
Document expected = Document.parse("{ name : { $eq : 123, $type : ['long'], $not : { $eq : 'foo' } } }");
98+
99+
Criteria c = Criteria.where("name") //
100+
.is(123) //
101+
.type(Type.INT_64)
102+
.not()
103+
.is("foo");
104+
105+
assertThat(c.getCriteriaObject()).isEqualTo(expected);
106+
}
107+
73108
@Test // GH-4850
74109
void testCombiningBsonRegexCriteria() {
75110

@@ -94,6 +129,20 @@ void testNotEqualCriteria() {
94129
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : { \"$ne\" : \"Bubba\"}}");
95130
}
96131

132+
@Test // GH-4606
133+
void testNotIsCriteria() {
134+
135+
Criteria c = new Criteria("name").not().is("Bubba");
136+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : { \"$not\" : { \"$eq\" : \"Bubba\"} } }");
137+
}
138+
139+
@Test // GH-4606
140+
void testNegatedEqualsCriteria() {
141+
142+
Criteria c = new Criteria("name").not().eq("Bubba");
143+
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : { \"$not\" : { \"$eq\" : \"Bubba\"} } }");
144+
}
145+
97146
@Test
98147
void buildsIsNullCriteriaCorrectly() {
99148

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ void testQueryWithNot() {
5757
.isEqualTo(Document.parse("{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}"));
5858
}
5959

60-
@Test
61-
void testInvalidQueryWithNotIs() {
62-
63-
assertThatExceptionOfType(InvalidMongoDbApiUsageException.class)
64-
.isThrownBy(() -> new Query(where("name").not().is("Thomas")));
65-
}
66-
6760
@Test
6861
void testOrQuery() {
6962

0 commit comments

Comments
 (0)