Document updates using mongo-ruby-driver?
Asked Answered
A

1

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)

Azoth answered 22/1, 2011 at 20:5 Comment(0)
S
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"}})
Synovitis answered 26/1, 2011 at 7:44 Comment(1)
Also, if you want to update multiple documents: coll.update({<search criteria>}, {"$set" => {<update>}}, {:multi => true }) Just thought I'd add that since I couldn't find it anywhere.Divinize

© 2022 - 2024 — McMap. All rights reserved.