mongoid update all documents with conditions
14,403
You should try to update your MongoID to latest version. Mongoid 2.0 was released sometime back. I guess update_all, destroy_all and delete_all got introduced in one of the rc's.
After upgrade, following should work
Employee.where(:address1 => 'Calgary').update_all(:salary => 10000)
Related videos on Youtube
Author by
Gagan
Updated on March 30, 2020Comments
-
Gagan about 4 years
I have a model
class Employee include Mongoid::Document field :first_name field :last_name field :address1 field :address2 field :salary end
Now I need to update all Employee's salary to 10000 whose address1 is "Calgary"
Now I tried this query
Employee.update_all "salary = 10000", "address1 = 'Calgary'"
But this query gave me error as:
NoMethodError: undefined method `update_all' for Employee:Class
Thanks
-
berkes about 13 yearsAFAIK the gotcha is that
update_all
is a method on a ResourceList (array of results) and not on the model itself. So you can not call Foo.update_all, but you can call Foo.some_selection.update_all. -
Sergio Tulentsev over 8 years@berkes: you can get "empty" selection by using
Foo.scoped.update_all