2012-09-19 91 views
1

我已經閱讀了mongoDB文件,但沒有我能找到一個例子,概述了我想要做的事情。檢查是否在子陣列和刪除子陣列

的關閉我來就是:

> t.insert({x: [1,2,3,4,3,2,3,4]}) 
    > t.find() 
    { "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, 3, 4, 3, 2, 3, 4 ] } 
    > t.update({x:3}, {$unset:{"x.$":1}}) 
    > t.find() 
    { "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, null, 4, 3, 2, 3, 4 ] } 

但是什麼我想要做我的收藏看起來這不會工作像

array (
    'offers' => 
    array (
    0 => 
    array (
     'validto' => 'Thurs', 
     'validfrom' => 'Mon', 
     'percentage' => '0.05', 
     'cashback' => '2', 
     'minspend' => '40', 
     'status' => 'pending', 
     '_id' => '1347940686722', 
    ), 
    1 => 
    array (
     'validto' => 'Thurs', 
     'validfrom' => 'Mon', 
     'percentage' => '0.06', 
     'cashback' => '3', 
     'minspend' => '50', 
     'status' => 'pending', 
     '_id' => '1347940705277', 
    ), 
    2 => 
    array (
     'validto' => 'Sun', 
     'validfrom' => 'Fri', 
     'percentage' => '0.04', 
     'cashback' => '2', 
     'minspend' => '50', 
     'status' => 'pending', 
     '_id' => '1347940730663', 
    ), 
    3 => 
    array (
     'validto' => 'Sun', 
     'validfrom' => 'Fri', 
     'percentage' => '0.05', 
     'cashback' => '3', 
     'minspend' => '60', 
     'status' => 'pending', 
     '_id' => '1347940743513', 
    ), 
    4 => 
    array (
     'validto' => 'Sun', 
     'validfrom' => 'Fri', 
     'percentage' => '0.05', 
     'cashback' => '5', 
     'minspend' => '100', 
     'status' => 'pending', 
     '_id' => '1347940755230', 
    ), 
), 
    'store_id' => new MongoId("5046cd963cd202c49d3140e7"), 
) 

如何,如果我想刪除3陣列(但這裏的關鍵是我不知道,如果它的0-4陣列,我所有的是該子數組內的數據和收集行的_id

這是我在想什麼,但我被卡住了

$collection->update(array("store_id"=>$this->data['_id'], array("offers._id"=>1347940686722)), array('$unset'=>array("offers.$"=>1))); 

回答

1

這是我如何解決它

$collection->update(array("store_id"=>new MongoId("5046cd963cd202c49d3140e7")), array('$pull'=>array("offers" =>array("id"=>"1347940755230"),false,false)));