MongoDB Spring 数据中的 $addField 和 $indexOfArray
MongoDB $addField and $indexOfArray in Spring Data
我正在尝试根据 Asya Kamsky 的 post:
使用 MongoDB 和 Spring 数据实现自定义排序
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.addFields().addField("scorrrz")
.withValueOfExpression("{ \"$indexOfArray\" : [ [\"John\", \"Bill\"], \"$name\" ] }").build());
当我尝试执行此操作时,我得到:
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(,)'
这语法不对吗?如何使用 Spring 数据完成此操作?
Collection<String> nameList = Arrays.asList("John", "Bill");
Aggregation agg = newAggregation(
addFields()
.addField("scorrrz").withValue(arrayOf(nameList).indexOf("$name"))
.build()
);
聚合的投影是一个 $addFields
阶段,具有 $indexOfArray
聚合数组操作。这将return一个字段scorrrz
,当没有匹配时,它将具有索引值或 -1
。这个 运行 与 Spring Boot v2.3.10 和 MongoDB v4.2.8 没问题。
运行 此聚合将管道 agg
传递给 MongoTemplate#aggregate
方法。
我正在尝试根据 Asya Kamsky 的 post:
使用 MongoDB 和 Spring 数据实现自定义排序List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.addFields().addField("scorrrz")
.withValueOfExpression("{ \"$indexOfArray\" : [ [\"John\", \"Bill\"], \"$name\" ] }").build());
当我尝试执行此操作时,我得到:
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(,)'
这语法不对吗?如何使用 Spring 数据完成此操作?
Collection<String> nameList = Arrays.asList("John", "Bill");
Aggregation agg = newAggregation(
addFields()
.addField("scorrrz").withValue(arrayOf(nameList).indexOf("$name"))
.build()
);
聚合的投影是一个 $addFields
阶段,具有 $indexOfArray
聚合数组操作。这将return一个字段scorrrz
,当没有匹配时,它将具有索引值或 -1
。这个 运行 与 Spring Boot v2.3.10 和 MongoDB v4.2.8 没问题。
运行 此聚合将管道 agg
传递给 MongoTemplate#aggregate
方法。