ruby on rails - Atomic inc of multiple mongoid hash values -
i have mongoid model hash field looks this:
class dimensionstat include ::mongoid::document include ::mongoid::timestamps field :data, type: hash attr_accessible :data end
i pretend on runtime fill data like
data: { 'a' => 1, 'b' => 2, ... }
i need perform increments on multiple keys atomically such as:
'a' => -1, 'b' => 5
somewhere found that:
instance.collection.find(_id: my_id).update("$inc" => {'data.a' => -1, 'data.b' => 5})
will trick doesn't, doing wrong?
update: i'm using mongoid 3.1.6
the .inc
method works non-existing keys, document not automatically refreshed database. try:
dimension_stat.inc('data.a' => -1, 'data.b' => 5) puts dimension_stat.data['a'] # not changed dimension_stat.reload puts dimension_stat.data['a'] # changed
you see counters have changed.
Comments
Post a Comment