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)
Update Documents in a Collection To update a document, MongoDB provides update operators, such as $set , to modify field values. <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ...
MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
db.collection.update(query, update, options) Modifies an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter. By default, the db. collection.
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"}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With