MongoDB 与 PHP 驱动程序 - 如何通过一个更新查询推送到多个数组?
MongoDB with PHP driver - how to push onto multiple arrays with one update query?
我想使用 PHP 中的单个 MongoDB 更新查询推送到同一文档中包含的多个数组。我尝试了以下两个调用但均未成功:
//this one causes php run error
$collection->update(array("scienceNumber"=>$scienceNumber), array('$push'=>array(array("comments"=> $comment), array("ids"=> $userID), array("times"=> $time))), array("upsert"=>true));
//this one runs a query but only pushes $time onto the times array, ignoring the other push instructions
//that outcome totally makes sense since I am assigning all these different arrays to the same key...no complaints...but then the above didn't work either
$collection->update(array("scienceNumber"=>$scienceNumber), array('$push'=>array("comments"=> $comment), '$push'=>array("ids"=> $userID), '$push'=>array("times"=> $time) ), array("upsert"=>true));
那么如何在一个更新查询中完成呢?
我还意识到我可以通过拥有一个文档数组并将所有值存储在一起来做到这一点,但这会使以后的其他查询更加麻烦。
这个怎么样:
$collection->update(
array("scienceNumber"=>$scienceNumber),
array('$push'=>array("comments"=> $comment, "ids"=> $userID, "times"=> $time)),
array("upsert"=>true)
);
我想使用 PHP 中的单个 MongoDB 更新查询推送到同一文档中包含的多个数组。我尝试了以下两个调用但均未成功:
//this one causes php run error
$collection->update(array("scienceNumber"=>$scienceNumber), array('$push'=>array(array("comments"=> $comment), array("ids"=> $userID), array("times"=> $time))), array("upsert"=>true));
//this one runs a query but only pushes $time onto the times array, ignoring the other push instructions
//that outcome totally makes sense since I am assigning all these different arrays to the same key...no complaints...but then the above didn't work either
$collection->update(array("scienceNumber"=>$scienceNumber), array('$push'=>array("comments"=> $comment), '$push'=>array("ids"=> $userID), '$push'=>array("times"=> $time) ), array("upsert"=>true));
那么如何在一个更新查询中完成呢?
我还意识到我可以通过拥有一个文档数组并将所有值存储在一起来做到这一点,但这会使以后的其他查询更加麻烦。
这个怎么样:
$collection->update(
array("scienceNumber"=>$scienceNumber),
array('$push'=>array("comments"=> $comment, "ids"=> $userID, "times"=> $time)),
array("upsert"=>true)
);