【问题标题】:$inc with mongoose ignored$inc 忽略了猫鼬
【发布时间】:2012-03-04 01:15:56
【问题描述】:

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

该项目被正确地推入数组中,并且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");
                    }
                });

【问题讨论】:

    标签: node.js mongodb express mongoose mongohq


    【解决方案1】:

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

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

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

    【讨论】:

    • 有没有办法减少而不是增加?像 $dec 吗?
    • @AdrienSchuler - 只需使用带有负数的 $inc - 比如{$inc : -1 }
    猜你喜欢
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2020-10-29
    • 2015-11-18
    相关资源
    最近更新 更多