1

Basically, I have two fields "totalAmountWithShipping" and "totalAmountPlusShipping" and want to project "totalAmountWithShipping" to a new field called "realTotalAmount" if "totalAmountWithShipping" is greater than 0, and project "totalAmountPlusShipping" to "realTotalAmount" otherwise.

I tried this:

project("day", "month", "year")
                    .and(
                        ConditionalOperators.`when`(
                            ComparisonOperators.Eq.valueOf("totalAmountWithShipping").equalToValue(0.0)
                    ).thenValueOf("totalAmountPlusShipping").otherwiseValueOf("totalAmountWithShipping")).`as`("realTotalAmount")

but always get 0.0 on "realTotalAmount" if the condition is true (i.e. if I project "totalAmountPlusShipping"). It works as it should when if the condition is false though.

If I try:

project("day", "month", "year")
                    .and(
                        ConditionalOperators.`when`(
                            ComparisonOperators.Eq.valueOf("totalAmountWithShipping").equalToValue(0.0)
                    ).then(100.0).otherwise(1.0)).`as`("realTotalAmount")

it works as expected.

1 Answers1

0

Use '$' with the field name since you're passing reference to that field. Following query should work:

project("day", "month", "year")
                    .and(
                        ConditionalOperators.when(
                            ComparisonOperators.Eq.valueOf("$totalAmountWithShipping").equalToValue(0.0)
                    ).thenValueOf("$totalAmountPlusShipping").otherwiseValueOf("$totalAmountWithShipping")).as("realTotalAmount")