Spring Data MongoDB: MergeOperation返回整个集合.为什么?



我正在运行一个MongoDB聚合,其中最后一个阶段是一个$merge,它将一些字段写入另一个集合。基本上,这个$merge应该只对单个文档起作用(这就是为什么我们有on: "_id"

下面是我用Java做的:

var merge = Aggregation.merge().intoCollection("items")
.on("_id")
.whenMatched(MergeOperation.WhenDocumentsMatch.mergeDocuments())
.whenNotMatched(MergeOperation.WhenDocumentsDontMatch.discardDocument())
.build();
var agg = Aggregation.newAggregation(match, group, merge);
var aggResult = mongoTemplate.aggregate(agg, "prices", Item.class);

聚合做它需要的,我可以看到所请求的文档已经改变,但是问题是aggregate()返回集合中的所有文档。

这是一个主要的缺点,当集合足够大时,它不能很好地扩展。

我如何改变我的查询,使它将只返回更改的文档。或者,如果不可能,只应用查询而不返回任何内容。

找到了答案,将与未来的读者分享:

mongoTemplate.aggregate(aggregation.withOptions(newAggregationOptions().skipOutput().allowDiskUse(true).build()), "collectionNme", EntityClass.class);

最新更新