1

我在 mongo 中有这个文件:

array (
  '_id' => new MongoId("509356edf275130917000000"),
  0 => 
  array (
    'id1' => '1000',
  ),
  1 => 
  array (
    'book' => 
    array (
      'id2' => '0',
      'name' => 'Example',
    ),
  ),
)

我想在“book”数组中添加一本书:

$coll->update(array(array("id1"=>'1000'),
     '$push'=>array('book'=>array(
        "id2"=>"1",
        "name" =>'War and peace'))));

但没有任何反应,有人知道该怎么做吗?

4

1 回答 1

0

book是单个数组。你不能更新。

根据给定条件更新记录

$collection->update($criteria, $newdata);

例子:

[
  '_id'     => new MongoId("509356edf275130917000000"),
  'ids'     => [1, 2, 3],
  'books'   => ['Book1', 'Book2']
]

$data = ['books' => 'Book3'];

$collection->update(

    // Criteria 
    ["_id"   => new MongoId("509356edf275130917000000")],

    // New Object 
    ['$push' => $data]
);
于 2012-11-02T09:17:25.657 回答