Search code examples
javamongodb

Add a new field to object while iterating through its collection


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

  1. get all the records for the document
  2. update each record for the new field
  3. 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.


Solution

  • 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" ] } } }
    ])