简体   繁体   English

Spring 数据 MongoDB:如何描述聚合 $merge 与 Spring 聚合?

[英]Spring Data MongoDB: How to describe aggregation $merge with Spring Aggregation?

Code that I want to execute by MongoTemplate :我想通过MongoTemplate执行的代码:

{
    $merge: {
        into: 'someCollection',
        on: "_id",
        whenMatched: 'merge',
        whenNotMatched: 'discard'
    }
}

I did not find any suitable methods that allow me to describe $merge stage, have doubts if Spring Data MongoDB even supports this stage?我没有找到任何合适的方法让我描述$merge阶段,怀疑Spring Data MongoDB是否支持这个阶段?

Yes, Spring Data MongoDB have support for $merge stage.是的, Spring Data MongoDB支持$merge阶段。 Your code can be executed by MongoTemplate following way. MongoTemplate可以通过以下方式执行您的代码。

MergeOperation mergeOperation = Aggregation.merge()
        .intoCollection("someCollection")
        .on("_id")
        .whenMatched(MergeOperation.WhenDocumentsMatch.mergeDocuments())
        .whenNotMatched(MergeOperation.WhenDocumentsDontMatch.discardDocument())
        .build();

Use this mergeOperation with mongoTemplate .将此mergeOperationmongoTemplate一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM