并非每个“新”功能都会使其立即进入诸如spring-mongo之类的抽象层。
因此,您需要做的就是定义一个使用该
AggregationOperation接口的类,该类将使用直接指定的BSON对象作为其内容:
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 aggregation = newAggregation( match( Criteria.where("username").is("user001") ), new CustomAggregationOperation( new BasicDBObject( "$lookup", new BasicDBObject("from", "NewFeedContent") .append("localField","content.contentId") .append("foreignField", "_id") .append("as", "NewFeedContent") ) ))该图显示了与内置
match()管道帮助程序混合的自定义类。
在每个帮助器下发生的所有事情都是将它们序列化为BSON表示形式,例如
DBObject无论如何。因此,这里的构造函数只是直接获取对象,并直接从将该对象返回
.toDBObject(),这是在序列化pipline内容时将被调用的接口上的标准方法。



