Skip to content

Commit 8fc8f84

Browse files
Add Criteria.isNullValue() as alternative to Criteria.is(null).
1 parent 239b191 commit 8fc8f84

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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

+37
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.stream.Collectors;
3030

3131
import org.bson.BsonRegularExpression;
32+
import org.bson.BsonType;
3233
import org.bson.Document;
3334
import org.bson.types.Binary;
3435
import org.springframework.data.domain.Example;
@@ -188,6 +189,42 @@ public Criteria is(@Nullable Object value) {
188189
return this;
189190
}
190191

192+
/**
193+
* Creates a criterion using {@literal null} equality comparison which matches documents that either contain the item
194+
* field whose value is {@literal null} or that do not contain the item field.
195+
* <p />
196+
* Use {@link #isNullValue()} to only query for documents that contain the field whose value is equal to
197+
* {@link org.bson.BsonType#NULL}. <br />
198+
* Use {@link #exists(boolean)} to query for documents that do (not) contain the field.
199+
*
200+
* @return this.
201+
* @see <a href="https://docs.mongodb.com/manual/tutorial/query-for-null-fields/#equality-filter">Query for Null or
202+
* Missing Fields: Equality Filter</a>
203+
* @since 3.3
204+
*/
205+
public Criteria isNull() {
206+
return is(null);
207+
}
208+
209+
/**
210+
* Creates a criterion using a {@link org.bson.BsonType} comparison which matches only documents that contain the item
211+
* field whose value is equal to {@link org.bson.BsonType#NULL}.
212+
* <p />
213+
* Use {@link #isNull()} to query for documents that contain the field with a {@literal null} value or do not contain the
214+
* field at all. <br />
215+
* Use {@link #exists(boolean)} to query for documents that do (not) contain the field.
216+
*
217+
* @return this.
218+
* @see <a href="https://docs.mongodb.com/manual/tutorial/query-for-null-fields/#type-check">Query for Null or Missing
219+
* Fields: Type Check</a>
220+
* @since 3.3
221+
*/
222+
public Criteria isNullValue() {
223+
224+
criteria.put("$type", BsonType.NULL.getValue());
225+
return this;
226+
}
227+
191228
private boolean lastOperatorWasNot() {
192229
return !this.criteria.isEmpty() && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1]);
193230
}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1260,14 +1260,25 @@ void resolvesFieldNameWithUnderscoreOnNestedMappedFieldnameWithUnderscoresCorrec
12601260
@Test // GH-3633
12611261
void mapsNullValueForFieldWithCustomTargetType() {
12621262

1263-
Query query = query(where("stringAsOid").is(null));
1263+
Query query = query(where("stringAsOid").isNull());
12641264

12651265
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
12661266
context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
12671267

12681268
assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", null));
12691269
}
12701270

1271+
@Test // GH-3633
1272+
void mapsNullBsonTypeForFieldWithCustomTargetType() {
1273+
1274+
Query query = query(where("stringAsOid").isNullValue());
1275+
1276+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
1277+
context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
1278+
1279+
assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", new org.bson.Document("$type", 10)));
1280+
}
1281+
12711282
class WithDeepArrayNesting {
12721283

12731284
List<WithNestedArray> level0;

0 commit comments

Comments
 (0)