是的,这是当前spring-mongo聚合助手的构造所不允许的。但您可以构建自己的“自定义”聚合操作Object,然后在管道中使用它:
public class CustomAggregationOperation implements AggregationOperation {
private DBObject operation;
public CustomAggregationOperation (DBObject operation) {
this.operation = operation;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
这里的用法是:
Aggregation agg = newAggregation(
new CustomAggregationOperation(
new BasicDBObject("$group",
new BasicDBObject("last",
new BasicDBObject("$last",
new BasicDBObject("fisrname","$firstname")
.append("lastname","$lastname")
)
)
)
)
);
因此,您可以只使用BSON对象构造器来按您所需的方式塑造管道阶段。这种快乐与
group()
和
project()
以及所有其他辅助运算符,因为它实现了
AggregationOperation
界面