Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 849
user471011 Avatar asked Jan 20 '26 10:01

user471011


1 Answers

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.

like image 198
Harshit Avatar answered Jan 22 '26 23:01

Harshit