4

我正在使用以下代码在数组中添加一些内容并增加两个不同的计数器。

该项目被正确推送到数组中,并且pendingSize 正确递增。但是 unRead 永远不会增加。它曾经增加,然后今天它停止了。我的 mongodb 集合(托管在 mongohq 上)中 unRead 字段的值设置为 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");
                    }
                });
4

1 回答 1

12

将 $inc 的 args 组合成一个嵌套对象,如下所示:

$inc: { unRead : 1, pendingSize: 1 }

JSON 中表示的对象是 key:value,其中的键必须是唯一的,因此尝试指定多个值是$inc行不通的。

于 2012-03-04T01:25:44.027 回答