Reputation: 3640
Is there a way to apply mongo's findAndModify to every document that matches the criteria and not just the first one? I couldn't find it in on mongodb.org.
Thanks.
Upvotes: 2
Views: 152
Reputation: 11129
The findAndModify() operation will only apply to one document at a time. If you want it to apply to multiple documents, you have to query for those documents using a find() operation, and apply findAndModify() to each one of those documents at one time.
Note that while individual findAndModify() commands are atomic, the set of findAndModify() commands that you run from the above loop are not atomic.
Upvotes: 1
Reputation: 21682
From the docs page:
This command can be used to atomically modify a document (at most one) and return it
So, no you can only operate on one at a time here. Hence, unless you iterate through every document you will not be able to use findAndModify this way. In addition, with the way findAndModify works, the performance of doing this on a large data set would be horrible.
What you really want to do is use the update command to achieve this type of thing instead:
http://www.mongodb.org/display/DOCS/Updating/#Updating-update%28%29
Upvotes: 2