RameshVel
RameshVel

Reputation: 65877

Mongoid update with Positonal operator not working

I am trying to update the mongodb embedded collection using the $ positional operator from ruby mongoid, but it is not working. Below the mongoid query

Viewcounter.collection.update({:item_id=>BSON::ObjectId('yyyy'),'viewinfos.remote_ip' => 'xxxx'},{'$inc' => {'viewinfos.$.viewcount' => 1}})

After some more digging i found that no mongodb queries works with mongoid update.including below simple query

Item.collection.update({'_id' =>BSON::ObjectId('sss')},{:isused => false})

has anyone got a better way of doing the positional operator queries with mongoid?

EDIT

But as per the mongodb official ruby driver documentation, this should work. below the excerpt

coll.update({"_id" => doc["_id"]}, {"$set" => {"name" => "MongoDB Ruby"}})

Upvotes: 1

Views: 425

Answers (1)

Tyler Brock
Tyler Brock

Reputation: 30146

The general idea is you would drop down to the ruby driver (through the collection) and do it like this:

Viewcounter.collection.update({"viewinfos.remote_ip" => "xxxx"}, {:$inc => {"viewinfos.$.viewcount" => 1}})

Upvotes: 4

Related Questions