lfkwtz
lfkwtz

Reputation: 1017

How can I update all Mongo documents within an array of _ids?

I have an array of User _id's in foundStory.authors like so:

"authors": [
    {
        "$oid": "5814ef8cafc25327a572eee5"
    },
    {
        "$oid": "5814ef80afc25327a572eee4"
    }
],

I would like to run through that array of authors, and increment their scores by two. Currently, I am trying to do this with the following:

User.update({ _id: { $in: foundStory.authors } },{ $inc: { score : 2 } })

But, this is only incrementing the author at the last index of my array. From what I have read, I expected this to work. Any ideas?

Upvotes: 0

Views: 35

Answers (1)

lfkwtz
lfkwtz

Reputation: 1017

Figured it out... adding {multi: true} seemed to solve the problem.

User.update({ _id: { $in: foundStory.authors } },{ $inc: { score : 2 } }, { multi: true })

Upvotes: 1

Related Questions