Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test if a value is null in aggregation? #4845

Open
rChaoz opened this issue Nov 28, 2024 · 1 comment
Open

How to test if a value is null in aggregation? #4845

rChaoz opened this issue Nov 28, 2024 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@rChaoz
Copy link

rChaoz commented Nov 28, 2024

How can I check if a field reference/aggregation expression is null and obtain a boolean?

  • ConditionalOperators.ifNull("field").then(false) - does not return true/false
  • ComparisonOperators.valueOf("field").equalToValue(null) - throws NPE
  • BooleanOperators.Or.or(Fields.field("field")) - also returns true if value is 0

I just want to obtain $eq: ["$field", null]. Am I missing something?

Workaround

ArrayOperators.arrayOf(List.of("null", "missing")).containsValue((DataTypeOperators.typeOf("field")));

But this results in a different query, I'd like to obtain exactly $eq: ["$field", null].

@christophstrobl
Copy link
Member

Thanks for bringing this to our attention. There does not seem to be an easy way to do this other than providing your own AggregationExpression.

AggregationUpdate update = AggregationUpdate.update()
    .set(SetOperation.builder().set("field").toValue(new AggregationExpression() {
        @Override
        public org.bson.Document toDocument(AggregationOperationContext context) {
            ArrayList<Object> args = new ArrayList<>(2);
            args.add("$field");
            args.add(null);
            return new org.bson.Document("$eq", args);
        }
    }));

We'll look into lifting the restriction on the Eq expression.

@christophstrobl christophstrobl added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants