I have a collection that holds documents that contains three fields: first_name, last_name, and age. I\'m trying to figure out what query in Mongoose I can use to do a bulk
you can use array.map
instead use for
const result = await Model.bulkWrite(
documents.map(document => {
document = {
...document, ...{
last_update: Date.now(),
foo: 'bar'
}
}
return {
updateOne: {
filter: {document_id: document.document_id}, //filter for each item
update: {
$set: document,//update whole document
$inc: {version: 1}//increase version + 1
},
upsert: true //upsert document
}
}
}
));
Hope my answer HERE helps you out. It deals with bulk upsert for an ecommerce domain asynchronously