最好的方法是匹配数组元素,并$
使用 Bulk()
API
使用位置运算符分别进行更新。您真的不应该吹牛
qty。
var bulk = db.mycollection.initializeOrderedBulkOp(), count = 0;db.mycollection.find({ "pre" : "efg" }).forEach(function(doc){ var qty = doc["qty"]; for (var idx = 0; idx < qty.length; idx++){ bulk.find({ "_id": doc._id, "qty": { "$elemMatch": { "num": qty[idx]["num"]}} }).update({ "$set": { "qty.$.num": 0 }}) } count++; if (count % 200 == 0) { // Execute per 200 operations and re-init. bulk.execute(); bulk = db.mycollection.initializeOrderedBulkOp(); } })// Clean up queuesif (count % 200 != 0) bulk.execute();


