嗨,我是 mongodb 的新手,我在这里使用 PHP。我想使用如下所示的数组将子文档更新为现有文档。
[weeksAtOne] => Array
(
[156] => 1586
)
当我尝试调用以下函数时
$query = array('decade' => array('$in' => array(1980,1990)));
$songs->update($query,
array('$push'=>array('weeksAtOne'=>array(156 => 500))),
array('safe'=>true,'timeout'=>5000,'multiple'=>true))
它不会像预期的那样出现,而是会以不同的方式出现。请有人指导我这是更新此内容的正确方法。
原始数组:
Array
(
[_id] => MongoId Object
(
[$id] => 55004cbd30d6d48819000004
)
[decade] => 1980
[artist] => Olivia Newton-John
[song] => Physical
[weeksAtOne] => Array
(
[10] => 10
[11] => 100
[22] => 500
)
)
Array
(
[_id] => MongoId Object
(
[$id] => 55004cbd30d6d48819000005
)
[decade] => 1990
[artist] => Mariah Carey
[song] => One Sweet Day
[weeksAtOne] => Array
(
[10] => 16
[21] => 100
[23] => 500
)
)
预期输出:
Array
(
[_id] => MongoId Object
(
[$id] => 55004cbd30d6d48819000004
)
[decade] => 1980
[artist] => Olivia Newton-John
[song] => Physical
[weeksAtOne] => Array
(
[10] => 10
[11] => 100
[22] => 500
[156] => 1586
)
)
Array
(
[_id] => MongoId Object
(
[$id] => 55004cbd30d6d48819000005
)
[decade] => 1990
[artist] => Mariah Carey
[song] => One Sweet Day
[weeksAtOne] => Array
(
[10] => 16
[21] => 100
[23] => 500
[156] => 1586
)
)