使用sequelize和nodejs更新多个记录? [英] Updating multiple records using sequelize and nodejs?
本文介绍了使用sequelize和nodejs更新多个记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用node.js(v0.10.12)和续集(2.0.0-dev9),我想更新现有数据库表中的属性。在其他 SO讨论中,我只能更新一次一个记录。我将调用封装在一个for循环中,但是在整个for循环中更新了相同的记录。
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() {});
}
})
推荐答案
根据文档您需要执行BulkCreate :(未测试)
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) { ... });
这篇关于使用sequelize和nodejs更新多个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文