Add Criteria.isNullValue() as alternative to Criteria.is(null).
See #3633 Original pull request: #3643.
This commit is contained in:
committed by
Mark Paluch
parent
149a703ecc
commit
efa9a2d408
@@ -29,6 +29,7 @@ import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bson.BsonRegularExpression;
|
||||
import org.bson.BsonType;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.Binary;
|
||||
import org.springframework.data.domain.Example;
|
||||
@@ -188,6 +189,42 @@ public class Criteria implements CriteriaDefinition {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a criterion using {@literal null} equality comparison which matches documents that either contain the item
|
||||
* field whose value is {@literal null} or that do not contain the item field.
|
||||
* <p />
|
||||
* Use {@link #isNullValue()} to only query for documents that contain the field whose value is equal to
|
||||
* {@link org.bson.BsonType#NULL}. <br />
|
||||
* Use {@link #exists(boolean)} to query for documents that do (not) contain the field.
|
||||
*
|
||||
* @return this.
|
||||
* @see <a href="https://docs.mongodb.com/manual/tutorial/query-for-null-fields/#equality-filter">Query for Null or
|
||||
* Missing Fields: Equality Filter</a>
|
||||
* @since 3.3
|
||||
*/
|
||||
public Criteria isNull() {
|
||||
return is(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a criterion using a {@link org.bson.BsonType} comparison which matches only documents that contain the item
|
||||
* field whose value is equal to {@link org.bson.BsonType#NULL}.
|
||||
* <p />
|
||||
* Use {@link #isNull()} to query for documents that contain the field with a {@literal null} value or do not contain the
|
||||
* field at all. <br />
|
||||
* Use {@link #exists(boolean)} to query for documents that do (not) contain the field.
|
||||
*
|
||||
* @return this.
|
||||
* @see <a href="https://docs.mongodb.com/manual/tutorial/query-for-null-fields/#type-check">Query for Null or Missing
|
||||
* Fields: Type Check</a>
|
||||
* @since 3.3
|
||||
*/
|
||||
public Criteria isNullValue() {
|
||||
|
||||
criteria.put("$type", BsonType.NULL.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
private boolean lastOperatorWasNot() {
|
||||
return !this.criteria.isEmpty() && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1]);
|
||||
}
|
||||
|
||||
@@ -1258,7 +1258,7 @@ public class QueryMapperUnitTests {
|
||||
@Test // GH-3633
|
||||
void mapsNullValueForFieldWithCustomTargetType() {
|
||||
|
||||
Query query = query(where("stringAsOid").is(null));
|
||||
Query query = query(where("stringAsOid").isNull());
|
||||
|
||||
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
|
||||
context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
|
||||
@@ -1266,6 +1266,17 @@ public class QueryMapperUnitTests {
|
||||
assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", null));
|
||||
}
|
||||
|
||||
@Test // GH-3633
|
||||
void mapsNullBsonTypeForFieldWithCustomTargetType() {
|
||||
|
||||
Query query = query(where("stringAsOid").isNullValue());
|
||||
|
||||
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
|
||||
context.getPersistentEntity(NonIdFieldWithObjectIdTargetType.class));
|
||||
|
||||
assertThat(document).isEqualTo(new org.bson.Document("stringAsOid", new org.bson.Document("$type", 10)));
|
||||
}
|
||||
|
||||
class WithDeepArrayNesting {
|
||||
|
||||
List<WithNestedArray> level0;
|
||||
|
||||
Reference in New Issue
Block a user