In my application, we have mongo db for data persistence. As part of some changes, one document is modified and we are adding a new field to it. I need to write a update job in Java.
For example let’s call the document as Employee and I am adding a new field "FullName".
The update job will do the following
- get all the records for the document
- update each record for the new field
- call MongoRepository.saveAll method
I want to know if there is an alternative way to do it?
below is the code
var newEmployees = new ArrayList<Employee>();
employeeRepository.findAll().forEach(employee -> {
employee.setFullName(employee.getFirstName() + " " + employee.getLastName());
newEmployees.add(employee);
});
employeeRepository.saveAll(newEmployees);
Note – we don’t perform direct db updates through mongo shell as during prod release, the support team needs extra instructions and support for such scenarios.
2
Answers
For performance reason you should consider bulkWrite. Would be similar to this (in JavaScript):
You should also consider
updateMany
with aggregation pipeline, for example like this:Your solution still always working great, but you should be aware of issues that can happens in case of having mass data.
you might want to consider batch processing to handle the updates in smaller chunks. This approach can prevent memory issues and optimize database operations.