Spring 数据 Mongo:IllegalArgumentException 试图对 minus() 使用字段引用

Spring Data Mongo: IllegalArgumentException trying to use field reference for minus()

我正在尝试使用 $subtract 运算符将两个字段之间的差异投射为使用以下代码行的单独的第三个字段:

pipeline.add(Aggregation.project("createdTime","modResult").andExpression("createdTime").minus("modResult").as("bucketedTime"));

但是,当我尝试执行聚合时,出现以下异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: can't serialize class org.springframework.data.mongodb.core.aggregation.Fields$AggregationField

我在这里做错了什么?我注意到,如果我提供整数而不是字段名称,就没有问题。感谢您的帮助。

根据我的问题 here,事实证明我应该使用 and() 而不是 andExpression()。后者试图将参数解释为表达式,例如 field1 + field2 作为 .and("field1").plus("field2").

的更方便的方式