为什么会报错
MongoError:多重更新仅适用于 $ 运算符
同时bulk find and update
用于更新多个文档。
试过:
var bulk = db.collection('users').initializeUnorderedBulkOp();
var emails = ['a@gmail.com', 'b@gmail.com', 'c@gmail.com','d@gmail.com'];
var allStatus = ['s1', 's2', 's3', ''];
for (var i =0; i< allStatus.length; i++) {
var query = { email: emails[i], status: { '$ne': allStatus[i] } };
var updateField = {};
if(allStatus[i]) {
updateField = {
$set: {status: allStatus[i], date: new Date()},
$push: {
notes: {note: 'jdf'+i, date: new Date()},
history: {status: allStatus[i], date: new Date()}
}
};
}
bulk.find(query).update(updateField);
}
bulk.execute();