3

I have the following MongoDB update operation, but it doesnt seem to work, anyone know why?

User.collection.update({ _id: BSON::ObjectId("5018ed448712ff240e0000a0") }, 
                       { "$set" => { name: "ben" } })

It does not throw an error, but just some integer which I am guessing is the doc size.

I am using Mongoid 2.4.10/Rails 3.2.7

1
  • 1
    User.collection.update({"_id"=>BSON::ObjectId('5018ed448712ff240e0000a0')}, { "$set" => {"name" => "ben" } }) Commented Aug 1, 2012 at 10:34

2 Answers 2

2

If you are using Mongoid, you coule just do a find and update:

User.find("5018ed448712ff240e0000a0").update_attributes!(name: "ben")

or you could use set:

User.find("5018ed448712ff240e0000a0").set(:name, "ben")

Note that set() takes 2 arguments; it does not accept a hash as an argument

Sign up to request clarification or add additional context in comments.

Comments

0

Can you use mongoid API instead and use following command:

User.find("5018ed448712ff240e0000a0").set(name: "ben")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.