Let's describe the problem.
- You have an iteration based flow with some library (example.: AsyncJS)
- Async Flow can be interrupted at some iteration
- You want to be sure all the operatiosn in MongoDB are correctly stored
or maybe take a future decision if that operation should happen or no
In this case i can suggest you to use Bulk Operation
// array of operations
let batch = yourCollection.initializeUnorderedBulkOp();
async.each (yourArrayToIterate, function (item, next) {
// add some Operation that will be execute lated
batch.insert({firstName:"Daniele"});
batch.find({firstName: "Daniele"}).updateOne({$set: {lastName:"Tassone"}});
// other code to do...some Old Library
try {
someOldLibrary.doSomethingThatCanCreateAnExpection();
} catch (e) {
//some error happen
}
// more modern approach with New Library
someNewLibrary
.then(function (result){
})
.catch(function (err) {
// some error happen
}
}, function (err, result) {
// Flow is now completed, take a decision
if (err) {
// do nothing, maybe?
} else {
// execute all the Ordered Bulk
batch.execute().then(function(executionResult) {
// done, you can
});
}
});
Some Note:
1) BulkOperation can be Ordered and UnOrdered.
UnOrdered perform better and continue to work even if some error happen
2) Ordered is performed sequentially and if some error happen MongoD will not continue to the next element in the Bulk.
Documentation:
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#initializeOrderedBulkOp