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.
For more reference mongoDB you can follow this link below: