我有以下 mongodb 聚合:
[{$project: {
studentID : "$student.id",
bookId:{$cond: {
if: { $eq: [ "$type", "sourceName" ] },
then: "$from.id",
else: ""
}
}
}},
{$group: {
_id: '$studentID',
bookId :{"$push": "$bookId"}
}},
{$project: {
bookId :{
$filter:{
input:"$bookId",
as:"testBookID",
cond:{$ne: ["$$testBookID",""]}
}
}
}}]
由于在某些情况下 bookId 有可能为 null,因此在组结束时,会得到一个数组,该数组既有空值又有有效值,例如 [,,,,,bk001,,] 做最后一个项目来过滤掉不为空的值。 上面的聚合在指南针中运行良好并返回正确的值,但是当尝试在 Java Spring Boot 中实现时出现此异常: "$java" : org.springframework.data.mongodb.core.aggregation.MatchOperation@401b8515
Spring 启动代码:
Cond bookIdcond = ConditionalOperators.when(Criteria.where("type").is("sourceName"))
.thenValueOf("from.id").otherwise("");
ProjectionOperation projectOperation1 = Aggregation.project().and(bookIdcond).as("bookId");
GroupOperation groupOperation = Aggregation.group("studentID").push("bookId").as("bookId");
ProjectionOperation projectOperation2 = Aggregation.project()
.and(filter("bookId").as("testBookID").by(valueOf("testBookID").notEqualTo(""))).as("bookId");
在最后一次投影时出现错误。 执行最后一条投影线时出现异常: java.lang.IllegalArgumentException:AggregationField 名称不得为 null 或为空! 请帮忙提供代码。