mongodb mongoid-rails无法在嵌入文档的散列中增加计数器

我使用的是Mongoid 4.0.2,Rails 4.2.1和Mongodb 2.4.8。

我将daily_events作为人员集合中的嵌入文档,如下所示。

class Person include Mongoid::Document field :email, type: String field :month, type: Date field :monthly_total, type: Hash, default: {:email_open => 0, :email_click => 0, :page_view => 0} embeds_many :daily_events end class DailyEvent include Mongoid::Document field :name, type: String field :day_1, type: Hash, default: -> {:email_open => 0, :email_click => 0, :page_view => 0} field :day_2, type: Hash, default: -> {:email_open => 0, :email_click => 0, :page_view => 0} ...... field :day_31, type: Hash, default: -> {:email_open => 0, :email_click => 0, :page_view => 0} embedded_in :person end 

我想在day_1哈希值中增加email_open的值。 所以我尝试通过运行此命令命令来增加email_open计数:

 Person.first.daily_events.first.inc("day_1.email_open" => 1) 

在上面的mongoid命令下面尝试这样做

 {"$inc"=>{"daily_events.$.day_1.email_open"=>1}} 

它失败并出现以下错误:

  D, [2015-04-18T16:21:51.000555 #2918] DEBUG -- : MOPED: 54.14x.1x.xxx:10xxx COMMAND database=admin command={:ismaster=>1} runtime: 95.6645ms D, [2015-04-18T16:21:51.103361 #2918] DEBUG -- : MOPED: 54.14x.1x.xx:100xx QUERY database=msg collection=people selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 102.4067ms D, [2015-04-18T16:21:51.207447 #2918] DEBUG -- : MOPED: 54.14x.1x.xxx:10xxx UPDATE database=msg collection=people selector={"_id"=>BSON::ObjectId('552ef418646176111c000000'), "person_events._id"=>BSON::ObjectId('552ef42c646176111cc80000')} update={"$inc"=>{"person_events.$.day_1.email_open"=>1}} flags=[] D, [2015-04-18T16:21:51.208257 #2918] DEBUG -- : COMMAND database=msg command={:getlasterror=>1, :w=>1} runtime: 101.6476ms Moped::Errors::OperationFailure: The operation: # 1, :w=>1} @fields=nil> failed with error 10140: "Cannot apply $inc modifier to non-number"