Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Gateway to {@literal comparison expressions}.
*
* @author Christoph Strobl
* @author sathish256
* @since 1.10
*/
public class ComparisonOperators {
Expand Down Expand Up @@ -147,6 +148,18 @@ public Eq equalToValue(Object value) {
return createEq().equalToValue(value);
}

/**
* Creates new {@link AggregationExpression} that compares two values and returns {@literal true} when the first
* value is equal to {@literal null}.
*
* @return new instance of {@link Eq}.
* @since 5.1
*/
@Contract(" -> new")
public Eq isNull() {
return createEq().isNull();
}

@SuppressWarnings("NullAway")
private Eq createEq() {
return usesFieldRef() ? Eq.valueOf(fieldReference) : Eq.valueOf(expression);
Expand Down Expand Up @@ -505,6 +518,21 @@ public Eq equalToValue(Object value) {
Assert.notNull(value, "Value must not be null");
return new Eq(append(value, Expand.KEEP_SOURCE));
}

/**
* Creates new {@link Eq} with all previously added arguments appending {@literal null}. Renders to
* {@code {$eq: [..., null]}}.
*
* @return new instance of {@link Eq}.
* @since 5.1
*/
@Contract(" -> new")
public Eq isNull() {

List<Object> args = values();
args.add(null);
return new Eq(args);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Christoph Strobl
* @author Divya Srivastava
* @author Mark Paluch
* @author sathish256
*/
public class ProjectionOperationUnitTests {

Expand Down Expand Up @@ -1508,6 +1509,15 @@ void shouldRenderEqAggregationExpressionWithListComparison() {
assertThat(agg).isEqualTo(Document.parse("{ $project: { eq250: { $eq: [\"$qty\", [250]]} } }"));
}

@Test // GH-3473
void shouldRenderEqNullAggregationExpression() {

Document agg = project().and(ComparisonOperators.valueOf("qty").isNull()).as("isNull")
.toDocument(Aggregation.DEFAULT_CONTEXT);

assertThat(agg).isEqualTo(Document.parse("{ $project: { isNull: { $eq: [\"$qty\", null]} } }"));
}

@Test // DATAMONGO-1536
void shouldRenderGtAggregationExpression() {

Expand Down
Loading