7

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)

Community
  • 1
  • 1
Philip Wernersbach
  • 461
  • 1
  • 5
  • 11

1 Answers1

10

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"}})
spotman
  • 857
  • 7
  • 11
  • 2
    Also, if you want to update multiple documents: `coll.update({}, {"$set" => {}}, {:multi => true })` Just thought I'd add that since I couldn't find it anywhere. – GoldfishGrenade Dec 03 '13 at 01:31