I am working on a Spring Data MongoDB Aggregation Pipeline. The pipeline has multiple stages (match, unwind, project, group, addFields, etc.).
At all these stages, I just pass (totally undisturbed) an array of array of objects (say arr). Structure of object is somewhat like this:
{
quantity,
price_per_kg
}
At the second last stage, I unwind this arr. And at the last stage, I am projecting sum of quantity as total_quantity and sum of quantity * price_per_kg as total_amount.
Here’s my sample code:
Aggregation.project()
.and(ArrayOperators.Reduce.arrayOf("arr.arr1.quantity")
.withInitialValue(0).reduce(weightReduce)).as("total_quantity")
.and(ArrayOperators.Reduce.arrayOf("arr.arr1")
.withInitialValue(0).reduce(amtReduce)).as("total_amount")
AggregationExpression weightReduce = ArithmeticOperators.valueOf("$$value")
.add(ConvertOperators.valueOf(ConditionalOperators.ifNull("$$this").then(0)).convertToDouble());
AggregationExpression amtReduce = ArithmeticOperators.valueOf("$$value")
.add(ArithmeticOperators.valueOf(ConvertOperators.valueOf(ConditionalOperators.ifNull("$$this.quantity").then(0)).convertToDouble())
.multiplyBy(ConvertOperators.valueOf(ConditionalOperators.ifNull("$$this.price_per_kg").then(0)).convertToDouble()));
This aggregation pipeline results into the following error:
{
"status": "INTERNAL_SERVER_ERROR",
"timestamp": "10-06-2024 11:38:32",
"message": "Invalid reference '$$this.quantity'!",
"sub_errors": null,
"client_error_code": "E_001"
}
Now, I don’t understand why it is happening!
If I remove the total_amount projection the entire pipeline works fine, including the total_quantity projection. This time, it doesn’t result into invalid reference to quantity.
And if I execute the last two stages (unwind(arr) and the project() stage mentioned above) only, it produces the result, total_quantity and total_amount, as expected.
I’m unable to figure out the problem in this pipeline. I don’t understand what’s wrong happening with the quantity multiplied with price_per_kg. It work’s fine if I remove all the above stages. But, total_quantity is working with all the stages enabled, it means reference to quantity shouldn’t be inavlid.
At last, passing arr totally undisturbed.
Please help me figuring out the problem.
Thanks
Narayan