Spring Data MongoDB – Upsert

By | September 1, 2018

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.

@Autowired
private MongoTemplate mongoTemplate;

//search a document that doesn't exist
Query query = new Query();
query.addCriteria(Criteria.where("id").is("cimblo"));
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:

Leave a Reply

Your email address will not be published. Required fields are marked *