'spring data MongoDB maintaining data integrity in bulk update

I have a scenario where list of say 1000's employees (sample below) where their balance is updated at every month end where balance of each employee can be different.

{
  _id:1
  name:"John"
  balance:40
},

Now what would be best possible practice to perform same. Performing it one by one

for (Employee employee : employeeList) {
        employee.update();
    }

or

dropAll employees where id in (All employees ids)
mongoOperations.insert(employeeList, Employee.class);

or third approach could be

Load all employee records.
Insert employee records to a new collection say employee_temp.
Drop old collection (employee).
Rename newly inserted collection as old one (employee).

or is their any other way which can guarantee maximum success chances of database data integrity as well good from performance perspective.



Solution 1:[1]

Go with your first approach. Do it one by one. Instead of saving the entire object use atomic updates which will perform better.

Query query = Query.query(Criteria.where("id").is(id));
Update update = Update.update("balance", balance);
mongoTemplate.findAndModify(query, update, FindAndModifyOptions.options().returnNew(true), Employee.class);

Your Second and third will not scale. Dropping collection at run time is not advisable. The renaming collection has it's limitations.

Solution 2:[2]

One other possible approach on which we finally settled is to save our custom checkpoint in mongo for rollback or commit whatever is necessary (since our data size is small say 1000 employees only), we first create a document with old and new state for all employees and save it first before starting bulk update. If first record insertion is successful we have at least state of DB to which we can rollback or further try to re-execute bulk update in case of failures.

If the bulk update process completed with success, we drop our checkpoint collection, in case of failure we re-execute updates to all records where new value is not matching to employees record.

However if your design allows, better go for hybrid DB, in which save transactional data in some relational DB and for rest rely over mongo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 titogeo
Solution 2 tarunkumar