Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoid update all documents with conditions

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

like image 759
Gagan Avatar asked Apr 05 '11 05:04

Gagan


People also ask

Which method is used to update documents into a collection?

MongoDB's update() and save() methods are used to update document into a collection.


1 Answers

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)
like image 94
rubish Avatar answered Sep 25 '22 06:09

rubish