Reputation: 309
I'm having difficulty getting Model.bulkWrite
to upsert my documents. I haven't found anything in the documentation to suggest that this isn't possible (though, maybe I missed it). Below is a sample of the code I'm using:
var opts = [];
for (var item of data){
opts.push({
updateOne: {
filter: {"id": item.id},
update: {"$setOnInsert": {
"id": item.id,
"name": item.name}
}
}
})
};
Item.bulkWrite(
opts,
{upsert: true},
function(err, result){
if(err) throw err;
console.log(result);
// do more stuff here
}
);
I don't have any trouble running multiple update/upserts through a loop, but when I do that I lose access to a callback on when the operation is complete, so that doesn't work for me. I've also tested the above code replacing the updateOne
section with an insertOne
section and it works with no problems.
Does anyone have any ideas what's going on? Is upsert not a valid option here?
Upvotes: 1
Views: 8272
Reputation: 309
@Alex Blex, you were right on the money. Just an oversight on my part, I guess. For those interested this meant removing the upsert
from the second section of code and changing the first section to:
var opts = [];
for (var item of data){
opts.push({
updateOne: {
filter: {"id": item.id},
update: {"$setOnInsert": {
"id": item.id,
"name": item.name}
},
upsert: true
}
})
};
Upvotes: 8