You can use other bulk update APIs, such as the bulkWrite() method, which allows you to use an iterator to access the document, manage it, add the changed document to the list, and then send the list of update operations in the package to the server for execution.
An approach is shown below in which you would use the forEach() cursor to forEach() over a match and modify each document at the same time, pushing the update operation to a batch of about 1000 documents, which can then be immediately updated using bulkWrite() .
This is as efficient as using updateMany() , since it uses the same basic bulk write operations:
var cursor = db.myColl.find({"date": { "$exists": true, "$type": 1 }}), bulkUpdateOps = []; cursor.forEach(function(doc){ var newDate = new Date(doc.date); bulkUpdateOps.push({ "updateOne": { "filter": { "_id": doc._id }, "update": { "$set": { "date": newDate } } } }); if (bulkUpdateOps.length == 1000) { db.myColl.bulkWrite(bulkUpdateOps); bulkUpdateOps = []; } }); if (bulkUpdateOps.length > 0) { db.myColl.bulkWrite(bulkUpdateOps); }
source share