user471011
user471011

Reputation: 7374

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?

Upvotes: 3

Views: 1958

Answers (1)

Harshit
Harshit

Reputation: 1382

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.

Upvotes: 5

Related Questions