Is there a way to properly update an array of objects in a collection without having to remove and re-insert them? I am worried about losing data if the insert operation fails after removing the existing array. How can I efficiently update all documents in a collection?
Inserting an array of documents:
collection.insert(arrToInsert, function(err, results){
console.log(results.ops);
});
Current method of updating inserted array (not ideal):
collection.remove({}, function(err){
collection.insert(arrUpdated, function(err, result) {
console.log(results.ops);
});
});
I have attempted using collection.update but it does not support updating arrays.
Edit: For example: Insert this initial array:
[
{_id:0,name:"Harry"},
{_id:1,name:"Jacob"},
{_id:2,name:"Bob"}
]
and then transform it into:
[
{_id:0,name:"Dennis"},
{_id:1,name:"Peter"},
{_id:2,name:"Ghandi"}
]
My actual scenario is more complex as the array includes additional key/value pairs and requires multiple types of updates.