我使用以下代码在数组中添加一些内容并增加两个不同的计数器。
项目在数组中正确推送,pendingSize正确递增。但unRead永远不会增加。它曾经增加,然后今天它停止了。我的mongodb集合中的unRead字段的值(在mongohq上托管)设置为0(数字,而不是字符串)
当我查看我的控制台时,我看到'更新成功'。
任何线索,为什么它停止工作?
由于
Notifications.update({ "id" : theid}, { $push: { pending: notification}, $inc: { unRead : 1 }, $inc: { pendingSize: 1 }}, function(err){
if(err){
console.log('update failed');
callback("Error in pushing." + result.members[i]);
}
else{
console.log('update succes');
callback("Success");
}
});
答案 0 :(得分:12)
将$ inc的args合并到一个嵌套对象中,如下所示:
$inc: { unRead : 1, pendingSize: 1 }
JSON中表示的对象是key:value,其中键必须是唯一的,因此尝试为$inc
指定多个值将不起作用。