Search code examples
ruby-on-railsrubymongodbmongodb-rubynosql

Document updates using mongo-ruby-driver?


Assuming the following:

irb> x
irb> => {"_id"=> 123456, "welcome"=>"Hi!", "welcome2" => "Enjoy your stay!"}
irb> coll.class
irb> => Mongo::Collection

How can I use the raw mongo-ruby-driver to update the document corresponding to x by using both the rewriting method and the atomic update method? (See http://api.mongodb.org/ruby/current/file.TUTORIAL.html#Updating_a_Document)


Solution

  • given your example output, if you want to use the rewriting method it would be like this:

    coll.update({"_id" => x["_id"]}, x)
    

    or if you want to atomically change a value, it would be like this:

    coll.update({"_id" => x["_id"]}, {"$set" => {"welcome" => "Hello There"}})