使用sequelize 更新字段值怎么自动+1

使用sequelize 更新字段值怎么自动+1
比如我想更新文章的浏览人数,怎么自动更新浏览人数+1,而不用先获得当前浏览人数再去+1呢?

db.nices.update({lookcount:parseInt(req.query.lookcount)+1},{where:{id:id}}).then(function (resule) {
            console.log(resule);
            if(resule>0){`请输入代码`
                res.json({
                    code:200,
                    msg:"成功",
                    object:""
                })
            }
        })
阅读 12.4k
3 个回答

之前纠结过…然后直接上sequelize.literal了…

新手上路,请多包涵

user.update({

          buyNum: Sequelize.fn('1 + abs', Sequelize.col('user.buyNum'))
        },{
          where: {
            uid: uid
          }
        })

increment方法,可以为实例属性(字段)增加一个确定义的字面值,该方法直接单个或多个字段属性值的增加。

为一个属性增加值:

User.findById(1).then(function(user) {
  return user.increment('my-integer-field', {by: 2})
}).then(/* ... */)

为多个属性增加值:

User.findById(1).then(function(user) {
  return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}).then(/* ... */)

也可以使用对象的形式为多个属性增加值:

User.findById(1).then(function(user) {
  return user.increment({
    'my-integer-field':    2,
    'my-very-other-field': 3
  })
}).then(/* ... */)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题