Reputation: 683
If upsert:true is set then mongodb insert new document if exist. My below query working fine for single document. Unique Index:- {fid:1,uniqueid:1,atype:1,ftype:1}
db.Notification.updateMany(
{fid : 103,uniqueid:1001,atype:1,ftype:6}
,{ $set: { epoch: 1548484978658,actionbyuserid: 110, title: 'Good Morning To All'}}
,{upsert:true}
);
But when execute below query it is not inserting new documents for not matched documents;
db.Notification.updateMany(
{fid : {$in:[101,102,103]},uniqueid:1001,atype:1,ftype:6}
,{ $set: { epoch: 1548484978658,actionbyuserid: 110, title: 'Good Morning To All'}}
,{upsert:true}
)
Is there other to check and insert not found documents ?
Upvotes: 1
Views: 2434
Reputation: 46491
You can use bulkWrite
operation
Array you want to update with
const array = [101, 102, 103]
Query for bulk update
Model.bulkWrite(
array.map((val) =>
({
updateOne: {
filter: { _id: val, uniqueid: 1001, atype: 1, ftype: 6 },
update: { $set: { epoch: 1548484978658, actionbyuserid: 110, title: 'Good Morning To All'} },
upsert: true
}
})
)
})
Upvotes: 2