【发布时间】:2017-01-09 14:41:46
【问题描述】:
我有这个 mongodb 查询
db.getCollection('myCollection').aggregate(
[{
$project: {
length: {
$strLenCP: "$prefix"
}
}
}, {
$sort: {
length: -1
}
}]
)
我想在 Spring Java 项目中使用,但我无法编写正确的 Java 代码(排序不是问题)。
我试过了
Aggregation agg = newAggregation(project().andExpression("strLenCP(prefix)").as("prefixLength"));
AggregationResults < RequestSettingsWithPrefixLength > results = mongoTemplate.aggregate(agg, RequestSettings.class, RequestSettingsWithPrefixLength.class);
List < RequestSettingsWithPrefixLength > requestSettingsList = results.getMappedResults();
但我在 agg JSON(调试模式)中得到一个空键:
{
"aggregate": "__collection__",
"pipeline": [{
{
"$project": {
"prefixLength": {
"null": ["$prefix"]
}
}
}]
}
}
可以看到我的agg对象有这个投影操作:
expression -> strLenCP(prefix)
field -> AggregationField: AggregationField - name: prefixLength, target: prefixLength, synthetic: true
params -> []
我不确定这是否正确,但我找不到任何使用 strLenCP 的文档。 我只发现这个使用 strLenCP 投影的测试: https://github.com/spring-projects/spring-data-mongodb/blob/dc57b66adfd60b4d69d1d349b4fcfa4ab0da95e7/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java#L922
有人可以帮忙吗?
干杯
【问题讨论】:
-
感谢@Sagar Reddy 格式化
-
Np。你在哪个版本的 spring-mongo-db 上?
-
mongo-java-driver 版本是 3.4.0 和 org.springframework.data 是 1.9.5.RELEASE
标签: mongodb spring-data aggregation-framework