Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

node.js - Addition and Subtraction Assignment Operator With Sequelize

I would like to do an update by doing a simple addition on Sequelize.

table:

id || data
 1 ||  10 

sample:

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

after this query

id || data
 1 ||  11

I know it's a simple question, but I could not find the solution.

Which operator can I add and subtract? Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here it is :

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

OR

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
  })
});

You can also do decrement by just replacing increment with decrement.

For more detail : DO READ


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...