2

我正在使用 Mongoid 4.0.2、Rails 4.2.1 和 Mongodb 2.4.8。

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

 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: #      <Moped::Protocol::Command
   @length=67
   @request_id=1085
   @response_to=0
   @op_code=2004
   @flags=[]
   @full_collection_name="msg.$cmd"
   @skip=0
   @limit=-1
   @selector={:getlasterror=>1, :w=>1}
   @fields=nil>
   failed with error 10140: "Cannot apply $inc modifier to non-number"
4

0 回答 0