构造聚合
Aggregation aggregation = newAggregation(
match(beforeCriteria),
unwind("suborders"),
match(endCriteria),
sort(page.getSort()),
skip(page.getPageNumber()),
limit(page.getPageSize())
);
创建条件
Criteria beforeCriteria = where("suborders.supplierChannel.other.supplier._id").is(new ObjectId(sourceId));
Criteria endCriteria = where("suborders.supplierChannel.other.supplier._id").is(new ObjectId(sourceId));
if (Strings.isNotBlank(keywords)) {
endCriteria.orOperator(
where("code").regex(keywords),
where("member.code").regex(keywords),
where("member.name").regex(keywords),
where("status.name").regex(keywords));
}
查找Count总记录数
Aggregation countAggregation = newAggregation(
match(beforeCriteria),
unwind("suborders"),
match(endCriteria),
Count.count()
);
调用
@Autowired
private MongoOperations mongoOperations;
AggregationResults<OrderSource> orders = mongoOperations.aggregate(aggregation, Order.class, OrderSource.class);
List<OrderSource> orderSources = orders.getMappedResults();
AggregationResults<com.jfc.common.mongodb.Count> countResults = mongoOperations.aggregate(countAggregation,
Order.class, com.jfc.common.mongodb.Count.class);
long total = Count.get(countResults);
return new org.springframework.data.domain.PageImpl<>(orderSources, page, total);