Daily Archives: September 1, 2018

Spring Data MongoDB – Upsert

In this tutorial i will share to you how to use UpSert operation in spring data mongoDB.

If no document that matches the query, a new document is created by combining the query and update object.

private MongoTemplate mongoTemplate;

//search a document that doesn't exist
Query query = new Query();
Update update = new Update();
update.set("age", 21);
mongoTemplate.upsert(query, update, User.class);

Spring Data MongoDB – Upsert

The output is a new document created by combining both query and update object.

Spring Data MongoDB

For more reference mongoDB you can follow this link below: