Updating multiple records using sequelize and nodejs?

谁说我不能喝 提交于 2019-12-11 07:58:13

问题


Using node.js (v0.10.12) and sequelize (2.0.0-dev9), I am wanting to update attributes within an existing database table. From anther SO discussion I am able to update only one record at a time. I encapsulated the call in a for loop but that updated the same record throughout the for loop.

Could someone help me modify this code to update all records that match the search requirements? Below is my code:

global.db.Task.find({ where: {"Cd": "match"} && {"Desc": null}
                                      }).on('success', function(task) {                                 
                                          if (task) { // if the record exists in the db                                                               
                                              task.updateAttributes({
                                                  Desc: "New Upate"
                                              }).success(function() {});
                                              }
                                      })

回答1:


According to the docs you need to do a BulkCreate: (Not tested)

global.db.Task.update(
    {Desc: 'New Upate'},       //set attribute 
    {cd: 'match', Desc: null}  //where criteria 
  ).success(function(affectedRows) { ... });


来源:https://stackoverflow.com/questions/24471544/updating-multiple-records-using-sequelize-and-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!