jjr*_*jrz 2 mongodb spring-data aggregation-framework spring-data-mongodb
我正在尝试根据 Asya Kamsky 的帖子使用 MongoDB 和 Spring Data 实现自定义排序:
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.addFields().addField("scorrrz")
.withValueOfExpression("{ \"$indexOfArray\" : [ [\"John\", \"Bill\"], \"$name\" ] }").build());
Run Code Online (Sandbox Code Playgroud)
当我尝试执行此操作时,我得到:
ERROR a.insurance.misc.ErrorAttributes - /api/v1/insurance/opportunity/all
org.springframework.expression.spel.SpelParseException: Expression [{ "$indexOfArray" : [ ["John", "Bill"], "$name" ] }] @29: EL1043E: Unexpected token. Expected 'rsquare(])' but was 'comma(,)'
Run Code Online (Sandbox Code Playgroud)
这不是正确的语法吗?如何使用 Spring Data 来完成此操作?
Collection<String> nameList = Arrays.asList("John", "Bill");
Aggregation agg = newAggregation(
addFields()
.addField("scorrrz").withValue(arrayOf(nameList).indexOf("$name"))
.build()
);
Run Code Online (Sandbox Code Playgroud)
聚合的投影是$addFields具有$indexOfArray聚合数组操作的阶段。这将返回一个字段,并且当没有匹配时它将scorrrz有索引值。这在 Spring Boot v2.3.10 和 MongoDB v4.2.8 上运行良好。 -1
运行此聚合将管道传递agg给该MongoTemplate#aggregate方法。
| 归档时间: |
|
| 查看次数: |
2791 次 |
| 最近记录: |