How to update a document in mongo to get performance?

I am new to Spring Data Mongo . I have a scenario when I want to create a study if it is not already presented in mongo db. If it is already present, then I must update it with new values.

I tried as follows, which works fine in my case, but I'm not sure if this is the correct / best / recommended way to update, etc. regarding performance.

Can anyone be guided by this?

public void saveStudy(List<Study> studies) { for (Study study : studies) { String id = study.getId(); Study presentInDBStudy = studyRepository.findOne(id); //find the document, modify and update it with save() method. if(presentInDBStudy != null) { presentInDBStudy.setTitle(task.getTitle()); presentInDBStudy.setDescription(study.getDescription()); presentInDBStudy.setStart(study.getStart()); presentInDBStudy.setEnd(study.getEnd()); repository.save(presentInDBStudy); } else repository.save(study); } } 
+5
source share
3 answers

For this you need to use MongoTemplate.upsert() . You will need to add two more classes: StudyRepositoryCustom , which is an interface and a class that extends this interface, say StudyRepositoryImpl

 interface StudyRepositoryCustom { public WriteResult updateStudy(Study study); } 

Update current StudyRepository to extend this interface

 @Repository public interface StudyRepository extends MongoRepository<Study, String>, StudyRepositoryCustom { // ... Your code as before } 

And add a class that implements StudyRepositoryCustom . Here we @Autowire our MongoTemplate and provide an implementation for updating Study or save it if it does not exist. We use the MongoTemplate.upsert() method.

 class StudyRepositoryImpl implements StudyRepositoryCustom { @Autowired MongoTemplate mongoTemplate; public WriteResult updateStudy(Study study) { Query searchQuery = new Query(Criteria.where("id").is(study.getId()); WriteResult update = mongoTemplate.upsert(searchQuery, Update.update("title", study.getTitle).set("description", study.getDescription()).set(...)), Study.class); return update; } } 

Please note that StudyRepositoryImpl will be automatically loaded by the Spring data infrastructure, as we have followed a naming convention that extends the interface name of the main repository using Impl

Check out this example on github, for @Autowire -ing a MongoTemplate and using a custom repository as above.

I have not tested the code, but it will help you :-)

+2
source

You can use the upsert functions to do this, as described in the mongo documentation. https://docs.mongodb.com/v3.2/reference/method/db.collection.update/

+1
source

You can update your code to use <S extends T> List<S> save(Iterable<S> entites); to save all objects. Spring MongoRepository will take care of all possible cases based on the presence of the _id field and its value.

Further information here https://docs.mongodb.com/manual/reference/method/db.collection.save/

This will work just fine for basic save operations. You do not need to download a document to update. Just set the identifier and be sure to include all the fields for updating, updating it, replacing the existing document.

Simplified domain object:

 @Document(collection = "study") public class Study { @Id private String id; private String name; private String value; } 

Repository:

 public interface StudyRepository extends MongoRepository<Study, String> {} 

Imagine you have an existing entry with _id = 1

Collection status until:

 { "_id" : 1, "_class" : "com.mongo.Study", "name" : "saveType", "value" : "insert" } 

Run all possible cases:

 public void saveStudies() { List<Study> studies = new ArrayList<Study>(); --Updates the existing record by replacing with the below values. Study update = new Study(); update.setId(1); update.setName("saveType"); update.setValue("update"); studies.add(update); --Inserts a new record. Study insert = new Study(); insert.setName("saveType"); insert.setValue("insert"); studies.add(insert); --Upserts a record. Study upsert = new Study(); upsert.setId(2); upsert.setName("saveType"); upsert.setValue("upsert"); studies.add(upsert); studyRepository.save(studies); } 

Collection status after:

 { "_id" : 1, "_class" : "com.mongo.Study", "name" : "saveType", "value" : "update" } { "_id" : 3, "_class" : "com.mongo.Study", "name" : "saveType", "value" : "insert" } { "_id" : 2, "_class" : "com.mongo.Study", "name" : "saveType", "value" : "upsert" } 
+1
source

Source: https://habr.com/ru/post/1259558/


All Articles