【问题标题】:want to increment field value in mongodb using sails in node想要使用节点中的风帆增加 mongodb 中的字段值
【发布时间】:2019-09-19 07:37:49
【问题描述】:

我想使用sails(node)在更新查询中将字段值加1

Users.update({ code: referral }, { $inc: [{ referredCount: 1 }] }).then(function (update) {})
 I have tried this.

Users.update({ code: referral }, { $inc: [{ referredCount: 1 }] }).then(function (update) {})


 "err": {
            "error": "E_UNKNOWN",
            "status": 500,
            "summary": "Encountered an unexpected error",
            "raw": {
                "name": "MongoError",
                "message": "The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.",
                "driver": true,
                "index": 0,
                "code": 52,
                "errmsg": "The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage."
            }
        }

【问题讨论】:

  • 尝试 { $inc: { referCount: 1 }}
  • @sushantmehta 不工作
  • @AbhishekBansal 您的架构或数据是什么样的?在你正在表演的基础上。

标签: node.js mongodb sails.js


【解决方案1】:

这是唯一正确的方法。

Users.update({ code: referral }, { $inc: { referredCount: 1 } })

如果不递增,则检查文档中是否存在referredCount。
如果存在,则检查它是否没有嵌套在另一个对象中。
最后还要通过下面的查询检查是否有应用过滤器的实际文档。

Users.find({ code: referral })

【讨论】:

  • referredCount: { type: 'integer', defaultsTo: 0, },
  • 这是用户模型中的字段
  • 你能用 Users.find({ code: refer }) 找到记录吗?
  • 请检查更新后的代码。我认为你应该把推荐写成“推荐”,如果它不是动态的
  • 我可以使用 Users.find 找到记录记录,并且推荐是动态的
猜你喜欢
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 2014-05-17
  • 2018-07-27
  • 2020-02-08
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多