为什么getcounter
函数只被调用一次?
collect.update({sys_RowId: {$exists : false}},
{$set: {sys_RowId: getcounter()}},{multi: true});
是的,首先评估getcounter
函数。传递给collect.update
的JSON对象只包含其返回值。不可能将getcounter
作为回调传递给更新方法。
您需要在forEach()
循环中执行此操作:
collect.find(({sys_RowId: {$exists : false}}).forEach(function(doc){
collect.update(doc._id, {$set: {sys_RowId: getcounter()}});
});