kapso
kapso

Reputation: 11903

Mongodb simple update does not work in Ruby/Mongoid

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

Upvotes: 3

Views: 1275

Answers (2)

Shailen Tuli
Shailen Tuli

Reputation: 14171

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

Upvotes: 2

Iuri G.
Iuri G.

Reputation: 10630

Can you use mongoid API instead and use following command:

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

Upvotes: 0

Related Questions