Home > Back-end >  How to use updateMany with filter array?
How to use updateMany with filter array?

Time:04-29

How to use updateMany first check if url is present or not if present then update else insert. I can use it in updateOne in filter I can add {url: "https://url.com"} but when I am using updateMany how to check array of urls ["https://url1.com", "https://url2.com"]

CodePudding user response:

The common way to update multiple documents each with its own data, is to use a bulk update with upsert

An example using js is:

const urlDataBulk = urlDataModel.collection.initializeUnorderedBulkOp();
for (const oneUrlData of urlsDataArr)) {
  urlDataBulk.find({url: oneUrlData.url}).upsert().update({$set:{company: oneUrlData.company}});
}
urlDataBulk.execute()
  • Related