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

Model.where({}).update().exec() not updating #3221

Closed
joesonw opened this issue Jul 31, 2015 · 7 comments
Closed

Model.where({}).update().exec() not updating #3221

joesonw opened this issue Jul 31, 2015 · 7 comments
Milestone

Comments

@joesonw
Copy link

joesonw commented Jul 31, 2015

Tried

yield Model
        .where('myId'),equals(id)
        .update({a:'1'})
        .exec()

and

yield Model
        .where({'myId':id})
        .update({a:'1'})  
        .exec()

both returns

 {ok:0,n:0,nModified:0}

However, the following works

yield Model.update(query,update,options).exec()
@vkarpov15
Copy link
Collaborator

Try

yield Model
        .find()
        .where('myId'),equals(id)
        .update({a:'1'})
        .exec()

Off the top of my head I don't think Model even has a where() function, that's on the query builder..

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 3, 2015
@chazmo03
Copy link
Contributor

I am also encountering this bug. The query's update() method doesn't seem to do a good job of parsing the parameters when you call it promise-style without a callback. One workaround is to pass in an empty set of conditions, e.g., .update({}, {a:'1'}).

@vkarpov15
Copy link
Collaborator

@chazmo03 code example?

@tomi
Copy link

tomi commented Nov 7, 2015

I'm facing the same bug. Neither of these two work:

Model
    .where({ "_id": id })
    .update({ $inc: { views: 1 } })
    .exec(function (err, c) { ... });

Model
    .find()
    .where({ "_id": id })
    .update({ $inc: { views: 1 } })
    .exec(function (err, c) { ... });

Both return

 {ok:0,n:0,nModified:0}

Setting 'debug' to 'true' for mongoose shows that nothing is actually executed at all.

@vkarpov15
Copy link
Collaborator

@testtube- try just doing Model.update({ _id: id }, { $inc: { views: 1 } });. If that doesn't work, try executing the same query in the mongo shell.

@tomi
Copy link

tomi commented Nov 9, 2015

That did the trick. Is the chained version supported at all? At least the documentation shows it as an example: http://mongoosejs.com/docs/api.html#query_Query-update

@vkarpov15 vkarpov15 removed the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Nov 9, 2015
@vkarpov15 vkarpov15 added this to the 4.2.5 milestone Nov 9, 2015
@vkarpov15
Copy link
Collaborator

Yep the chained version should work in theory. I'll investigate.

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

No branches or pull requests

4 participants