Hi everyone, I’m trying to update multiple documents in collection by programmatically creating an update array for bulkWrite operation with mongoose.
let updateArr = [];
data.map((position) => {
position.quantities.map((quantity) => {
updateArr.push({
updateOne: {
filter: {
_id: mongoose.Types.ObjectId(position.position),
"quantities._id": mongoose.Types.ObjectId(quantity._id),
},
update: {
$set: {
"quantity.$.administratorAimedPrice":
quantity.administratorAimedPrice,
},
},
},
});
});
});
console.log(updateArr);
await Position.bulkWrite(updateArr);
This is how the array looks like:
[
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } },
{ updateOne: { filter: [Object], update: [Object] } }
]
What i’m getting back from mongoose:
POS TypeError: Update document requires atomic operators at OrderedBulkOperation.raw
If there is another way to update multiple documents, with different values by targeting different IDs, please share.
Thank you in advance.