Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating multiple database entries at once #2404

Closed
sahanDissanayake opened this issue Nov 16, 2014 · 7 comments
Closed

Updating multiple database entries at once #2404

sahanDissanayake opened this issue Nov 16, 2014 · 7 comments

Comments

@sahanDissanayake
Copy link

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

@sahanDissanayake
Copy link
Author

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

@sgress454
Copy link
Member

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).

@Clan-Utility
Copy link

According to the doc, the 2nd parameter of update() accepts [{}]. What does it mean?

@mcodev1
Copy link

mcodev1 commented Oct 15, 2015

@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()

@carpiediem
Copy link

@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.

@sgress454
Copy link
Member

@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 🙃

@carpiediem
Copy link

Cheers, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants