检查是否在子数组中并删除子数组数组

时间:2012-09-19 03:14:53

标签: php mongodb multidimensional-array collections

我已经阅读过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 个答案:

答案 0 :(得分:1)

以下是我如何解决它

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