spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

ComparisonOperators.Lte seems to match undefined/null value in switch caseOperator

Open Strat1987 opened this issue 2 years ago • 5 comments

ComparisonOperators.Lte seems to match undefined/null value in switch caseOperator

reactiveMongoTemplate.upsert(Query.query(
                                Criteria.where("id").is(b.getId())
                        ), AggregationUpdate.update()
                                .set("field.value").toValue(
                                        ConditionalOperators.switchCases(
                                                        CaseOperator.when(Gte.valueOf("field_priority")
                                                                        .greaterThanEqualToValue(b.getField().getPriority()))
                                                                .then("gte"),                                                
                                                        CaseOperator.when(Lte.valueOf("field_priority")
                                                                       .lessThanEqualToValue(b.getField().getPriority()))
                                                                .then("lt")
                                                )
                                                .defaultTo("default")
                                ), Bar.class)).flatMap(c -> Mono.just(foo));

if there is no document in the database we expect to run the default case, however we see the lte case is being ran. We've tried fiddling with the order and gt, lt methods but none of them seem to accomplish what we're after.

Strat1987 avatar Aug 30 '21 10:08 Strat1987

The matching behaviour is the same when running the query directly on the db. Have you tried adding another case like { case: { $eq : ["$field_priority", undefined] }, then: "default"} that will capture the null case?

christophstrobl avatar Aug 30 '21 14:08 christophstrobl

Thanks for the quick reply!

I've indeed came to a similar conclusion that it behaves the same on the datastore itself.

There doesn't seem a method that accepts undefined. I've tried BsonType.UNDEFINED but then I get a runtime exception and I can't find another way to perform the check within the switch statement.

Op ma 30 aug. 2021 16:32 schreef Christoph Strobl @.***

:

The matching behaviour is the same when running the query directly on the db. Have you tried adding another case like { case: { $eq : ["$field_priority", undefined] }, then: "default"} that will capture the null case?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-data-mongodb/issues/3791#issuecomment-908394111, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAYEJ57RWH2UHJPVOQLCR4TT7OJAJANCNFSM5DBSEHKA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Strat1987 avatar Aug 30 '21 16:08 Strat1987

Unfortunately $eq with BsonNull.VALUE does not seem to do the trick. A possible solution might be to make use of $ifNull like this

CaseOperator.when(Eq.valueOf(IfNull.ifNull("value").then("undefined")).equalToValue("undefined")).then("default")

christophstrobl avatar Aug 31 '21 06:08 christophstrobl

Thanks for your input. I've already tried and failed using CaseOperator.when(Eq.valueOf("field.property")).equalToValue(BsonType.UNDEFINED)).then("undef"),

ConditionalOperators.switchCases(

CaseOperator.when(Eq.valueOf(IfNull.ifNull("field.property").then("undefined")).equalToValue("undefined")).then("undef"), CaseOperator.when(Gte.valueOf("field.property") .greaterThanEqualToValue(5) .then("gte"), CaseOperator.when(Lt.valueOf("correlationId.priority") .lessThanValue(5) .then("lte") ) .defaultTo("default')

Unfortunately it still prints the defaultTo case when there is no document in the DB.

Solved it using:

CaseOperator.when(Eq.valueOf(Type.typeOf("correlationId_priority")).equalToValue("missing")) .then(b.getCorrelationId().getPriority()),

Really seems like quite a trivial case to have a helper method for imho.

Op di 31 aug. 2021 om 08:42 schreef Christoph Strobl < @.***>:

Unfortunately $eq with BsonNull.VALUE does not seem to do the trick. A possible solution might be to make use of $ifNull like this

CaseOperator.when(Eq.valueOf(IfNull.ifNull("value").then("undefined")).equalToValue("undefined")).then("default")

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-data-mongodb/issues/3791#issuecomment-908945463, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAYEJ56H7UO542WLX5X2YH3T7R2VJANCNFSM5DBSEHKA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Strat1987 avatar Aug 31 '21 08:08 Strat1987

@christophstrobl didn't quite understand, what's the enhancement that we want to do here?

tudormarc avatar May 25 '22 19:05 tudormarc