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)));
4

1 回答 1

1

这是我解决它的方法

$collection->update(array("store_id"=>new MongoId("5046cd963cd202c49d3140e7")), array('$pull'=>array("offers" =>array("id"=>"1347940755230"),false,false)));
于 2012-09-19T04:53:24.790 回答