Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 866
Philip Wernersbach Avatar asked Jan 22 '11 20:01

Philip Wernersbach


People also ask

Which operator is used to update documents in MongoDB?

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>, ... }, ...

Which function is used to save and update document in MongoDB?

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.

What is update query in MongoDB?

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.


1 Answers

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"}})
like image 94
spotman Avatar answered Oct 10 '22 05:10

spotman