如何通过Spring Data MongoDB聚合实现文档批量删除
基于MongoDB Aggregation批量删除匹配文档的实现问题
现有聚合操作代码
MatchOperation matchOperation = match(Criteria.where("group").is("A") .andOperator(Criteria.where("score").lt(50))) ; SortOperation sortOperation = sort(Sort.by(Direction.DESC, "department")); Aggregation aggregation = Aggregation.newAggregation(matchOperation, sortOperation) ;
需求与问题
需求:在Spring Boot应用中批量删除上述聚合查询匹配到的所有文档。
限制:仅允许使用Aggregation实现,不能切换为Query。
问题:找不到适配的实现方法。
已尝试方案
- 使用
mongoTemplate.findAllAndRemove():该方法仅接受Query参数,与当前使用的Aggregation不兼容。 - 使用BulkOperations:先执行
BulkOperations bulkOps = mongoTemplate.bulkOps(BulkMode.ORDERED, EntityName.class) ;,再调用bulkOps.remove(),但该方法同样需要List<Query>参数,无法适配Aggregation。
内容的提问来源于stack exchange,提问作者Kartik Gautam
相关产品推荐
相关产品推荐

