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
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.
For performance reason you should consider bulkWrite. Would be similar to this (in JavaScript):
var updates = [];
db.employeeRepository.find().forEach(x => {
updates.push(
{
updateOne: {
filter: { _id: x._id },
update: { $set: { FullName: employee.getFirstName() + " " + employee.getLastName() } }
}
});
})
db.employeeRepository.bulkWrite(updates, { ordered: false });
You should also consider updateMany
with aggregation pipeline, for example like this:
db.employeeRepository.updateMany({},[
{ $set: { FullName: { $concat: [ "$firstName", " ", "$lastName" ] } } }
])