PHP mongodb update - $ push array into array

时间:2015-07-27 20:06:19

标签: php mongodb

我尝试使用新事件更新Mongo文档中的"coordinates"属性。即。将"coordinates"数组(包含事件数组)与新的事件数组合并。

到目前为止我所拥有的:

$update = array('$push' => array("coordinates" => $events));

/** @var \MongoCollection $collection */
$collection = $db->$collectionName;
$return = $collection->update($conditions, $update, $options);
if ($return === false) {
   throw new \ErrorException('Unable to update collection');
}

这不会产生任何错误,但不会产生任何错误。上面的查询将$events数组作为数组附加到"coordinates"数组。

混淆?也许下面的图片会更好地解释..

enter image description here

也许有人可以帮助我找出我出错的地方!

1 个答案:

答案 0 :(得分:2)

您需要使用$each运算符

$update = array('$push' => array("coordinates" => array('$each' => $events)));
$return = $collection->update($conditions, $update, $options)
相关问题