Mongoose: Bulk upsert but only update records if they meet certain criteria Mongoose: Bulk upsert but only update records if they meet certain criteria mongoose mongoose

Mongoose: Bulk upsert but only update records if they meet certain criteria


This is how I do it :

        let bulkUpdate = MyModel.collection.initializeUnorderedBulkOp();        //myItems is your array of items        _.forEach(myItems, (item) => {            if (item !== null) {                let newItem = new MyModel(item);                bulkUpdate.find({ yyy: newItem.yyy }).upsert().updateOne(newItem);            }        });        await bulkUpdate.execute();

I think the code is pretty readable and understandable. You can adjust it to make it work with your case :)