MongoDB更新子文档问题

时间:2014-12-16 10:58:41

标签: php mongodb yii

我有像这样的Mongo结构,

"campaigns":[{
    "summary" : 
      [ { 
       "postcode" : [ { 
                       "id" : "71",
                       "name" : "Recall 1",
                        "qty" : 3, 
                       "history" : 
                            [ { "timestamp" : "2014-12-16 11:15:32",
                               { "timestamp":  "2014-12-16 11:15:53"
                            } ] 
                       } ] 
     },
     {
     "postcode" :
            [ {     
                "id" : "72", 
                "name" : "Recall 2", 
                "qty" : 1,
                "history" : [ { "timestamp" : "2014-12-16 11:15:53" } ] 
           } ] 
  }]

我正在努力i)增加postcode.id的数量:72 ii)为同一邮政编码ID 72插入另一个时间戳。

我的代码:

    $collection->update(array("campaigns.task.id" => $b,"_id"=> new MongoId($objectid),"campaigns.0.summary.0.postcode.0.id" => $a), array('$inc' =>  array('campaigns.0.summary.0.postcode.0.qty' => 1)));
    $collection->update(array("campaigns.task.id" => $b,"_id"=>new MongoId($objectid),"campaigns.0.summary.0.postcode.0.id" => $a),
 array('$addToSet' =>  array('campaigns.0.summary.0.postcode.0.history' => array("timestamp"=>$now->format('Y-m-d H:i:s')))));

但是postcode.id = 72没有得到更新,我对这个嵌套的子文档感到困惑,任何人都可以建议我解决方案吗?

0 个答案:

没有答案