我在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'))));
但没有任何反应,任何人都知道如何做到这一点?
答案 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]
);