[英]Combine Document and AggregationOperation Stages in a Spring MongoDB Aggregation Pipeline?
Spring Data MongoDB 為聚合階段提供了有用的構建器類,例如Aggregation.match()
或Aggregation.sort()
。 這些方法返回的對象實現了AggregationOperation
接口,該接口允許使用它們Aggregation.newAggregation()
創建一個新管道,然后將其傳遞給mongoTemplate.aggregate()
。
問題不是所有的 Mongo 聚合階段都有構建器方法,例如$lookup
和相關的子查詢。 在這些情況下,我必須手動創建一個帶有 JSON 描述階段的Document
。 但是我找不到同時使用AggregationOperation
和基於Document
的階段的方法。 有沒有辦法將Document
包裝成AggregationOperation
或類似的?
這是一個問題/功能直到今天仍然開放spring-data-mongodb - #3322
解決方法:實現AggregationOperation
接口
AggregationOperation lookup = new AggregationOperation() {
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document(...); // put here $lookup stage
}
};
AggregationOperation lookup = ctx -> new Document(...); // put here $lookup stage
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.