It is possible to insert an array of objects, but not possible to update them. You have to remove them and then insert them. I don't want to have to remove and then insert them, because if the remove goes well but then the insert has an error, the data is lost. How can I update an array (all documents) in a collection, properly?
Inserting array of documents:
collection.insert(arrToInsert, function(err, results){
console.log(results.ops);
});
Updating inserted array (not good):
collection.remove({}, function(err){
collection.insert(arrUpdated, function(err, result) {
console.log(results.ops);
});
});
I have tried using collection.update but this does not allow an array.
edit: For example: Insert this array:
[
{_id:0,name:"Harry"},
{_id:1,name:"Jacob"},
{_id:2,name:"Bob"}
]
and then, change it to this:
[
{_id:0,name:"Dennis"},
{_id:1,name:"Peter"},
{_id:2,name:"Ghandi"}
]
My actual case is a bit more complicated, because the array has another key/value pair, and can be changed in many ways.
.update()operations with.bulkWrite(). It's generally better to "show us" rather than talk about something you don't actually show, which is what your question presently does. See How to create a Minimal, Complete, and Verifiable example in the help center which gives you tips on how you structure a question so people understand what you mean and how to solve it.