Mongoid update with Positonal operator not working

438 Views Asked by At

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"}})
1

There are 1 best solutions below

2
On

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}})