代码之家  ›  专栏  ›  技术社区  ›  user7453724

带Sequelize的加减赋值运算符

  •  2
  • user7453724  · 技术社区  · 7 年前

    我想通过在Sequelize上做一个简单的添加来进行更新。

    表格:

    id || data
     1 ||  10 
    

    示例:

    db.table.update({ data : 1 }, { where: { id: 1 }});
    

    在此查询之后

    id || data
     1 ||  11
    

    我知道这是一个简单的问题,但我找不到解决办法。

    我可以使用哪个运算符进行加减运算?非常感谢。

    2 回复  |  直到 7 年前
        1
  •  5
  •   Vivek Doshi    3 年前

    这是:

    db.table.update({ field: Sequelize.literal('data + 1') }, { where: { id: 1 }}))
    

    User.findById(1).then(user => {
      // -----> First Way
      return user.increment('my-integer-field', {by: 2});
      // -----> Second Way
      return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
      // -----> Third Way
      return user.increment({
         'my-integer-field':    2,
         'my-very-other-field': 3
      })
    });
    

    你也可以 decrement 只需更换 increment 具有 减量 .


    对于版本6:

    await User.increment({age: 5}, { where: { id: 1 } }) // Will increase age to 15
    await User.increment({age: -5}, { where: { id: 1 } }) // Will decrease age to 5
    

    有关更多详细信息: DO READ

        2
  •  0
  •   Amos    6 年前

    续集增量(&S);减量

    myIncrementFunc(id, incrementor) {
      User.findById(id).then(user => {
        return user.increment('tableColumnName', {by: incrementor})
      }).then(user => {})
    }
    

    代码取自sequelize实例教程,用于递增(&A);递减: http://docs.sequelizejs.com/manual/tutorial/instances.html