$ push不在Mongodb中插入数组

时间:2012-11-02 05:46:01

标签: mongodb push

我在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'))));

但没有任何反应,任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分: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]
);