Search code examples
ruby-on-rails-3mongodbmongoidmoped

update removed from moped::colllection - NoMethodError: undefined method `update' for #<Moped::Collection


I am new to working with mongodb & mongoid.. This is a rails app that was on mongoid 2 at some pont and have since moved to mongoid 3

I am trying to run the migrations, one of which has the following

Assessment.collection.update({'result_access' => {'$exist' => false}}, {'$set' =>   {'result_access' => 'all'}}, {:multi => true})

I am unsure how to update this for mongoid 3


Solution

  • First you have to find your selector, let rephrase you code

    From

    Assessment.collection.update({'result_access' => {'$exist' => false}}, {'$set' =>   {'result_access' => 'all'}}, {:multi => true})
    

    To

    Assessment.collection.find('result_access' => {'$exist' => false}).update({'$set' =>   {'result_access' => 'all'}, {:multi => true})
    

    Or more general:

    Assessment.collection.find("yourSelector").update("thingsToUpdate")