Reputation: 36273
Is this possible with Rails+Mongoid:
Model.update_all("position=position-1")
Upvotes: 1
Views: 787
Reputation: 4166
Sure you can! Without selection (update all collections):
Model.update_all(obsolete: false)
or with selection:
Model.where(:id.in => ids_array).update_all(obsolete: true)
Upvotes: 0
Reputation: 230521
While there's no direct equivalent to this idiom (set field to a some function of some fields of the document), there is a way to do this exact update.
Model.collection.update({}, # find all documents
{'$inc' => {:position => -1}}, # decrement position
:multi => true) # multi-update (update all)
This is using underlying mongodb driver to do the job. I don't know how to express this in Mongoid interface.
Upvotes: 2