Hi,
This is my function to update multiple database values at once
Menuitem.update([{id: 1},{position: 3}], [{id: 2},{position: 3}]).exec(function(err, updatedRecords) {
if (err) {
return res.send({message: 'Could not update the records', err: err}, 500);
}
if (updatedRecords) {
return res.send({ records: updatedRecords }, 200);
} else {
return res.notFound('Records not found');
}
});
Checked the docs at https://github.com/balderdashy/waterline-docs/blob/master/query-methods.md#update-search-criteria--values--callback-
But could not find how to update multiple records at once.
http://stackoverflow.com/questions/26952705/update-multiple-records-at-once-using-sailsjs
@sgress454
@particlebanana
I saw this http://stackoverflow.com/questions/21982571/updating-multiple-mongodb-records-in-sails-js#22003583
Now I can update multiple records with one value, But can I also make the values to be in an array and then update the record from the index of that array, for example as below
var ids = [1, 2];
var pos = [4, 3]
Menuitem.update({id: ids},{position: pos}).exec(function(err, updatedRecords) {
if (err) {
return res.send({message: 'Could not update the records', err: err}, 500);
}
if (updatedRecords) {
return res.send({ records: updatedRecords }, 200);
} else {
return res.notFound('Records not found');
}
});
Oh, my! That would be something. But no, Waterline doesn't support this.
Also, in the future please use the docs at http://sailsjs.org/#/documentation, and if you have a support question, post it on StackOverflow or our Google Group (this forum is for reporting bugs in the Sails core).
According to the doc, the 2nd parameter of update() accepts [{}]. What does it mean?
@sgress454
I reached this issue while trying to find out how to batch update, so I'm also very curious what that [{}] in the docs is referring to. I'm using Mongo as my database and it's throwing an error when I provide an array of objects as the second parameter of .update()
@sgress454 , if your answer is to check the documentation, you should really ensure that it addresses the issue. As the previous two posts show, the docs seem to indicate that this should work.
@carpiediem That is very fair. I made the mistake of assuming the docs were correct. They usually are! I've updated them in the docs repo, and will see to it that the site is re-deployed soon.
For future reference:
The _find criteria_ (first argument) for .update() can either be a dictionary (same kind as you'd use for .find()) or, as a shortcut, a primary key value. To update multiple records by primary key, use an "in" query, e.g. User.update({ id: {in: [1, 2, 3]} } ... or the shortcut User.update({ id: [1, 2, 3]}...
The _values_ (second argument) for .update() _must_ be a dictionary of values to set. Waterline doesn't support using an array as the second argument to set different values for different records.
The Sails 1.0 docs are correct, at least 馃檭
Cheers, thanks.
Most helpful comment
@sgress454
I reached this issue while trying to find out how to batch update, so I'm also very curious what that
[{}]in the docs is referring to. I'm using Mongo as my database and it's throwing an error when I provide an array of objects as the second parameter of.update()