简体   繁体   中英

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

Code that I want to execute by 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?

Yes, Spring Data MongoDB have support for $merge stage. Your code can be executed by MongoTemplate following way.

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

Use this mergeOperation with mongoTemplate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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