我只是想尝试更新MongoDB数据库文档中的特定字段。
我有以下代码:
$connection = new MongoClient("private");
$collection = $connection->testdb->deliveries;
// Use the ID to generate the actual MongoID
$realmongoid = new MongoId($id);
$cursor = $collection->findOne(array('_id' => $realmongoid));
所以这一切都很好,但当我尝试使用此代码更新文档中的特定字段时:
$arrayWithDriverInfo = array("filledBy" => $_SESSION['username']);
$cursor->update($arrayWithDriverInfo);
它不起作用。我收到此消息:致命错误:Call to a member function update() on array in...
这里发生了什么?
答案 0 :(得分:0)
<?php
$connection = new MongoClient("private");
$collection = $connection->testdb->deliveries;
$obj = $collection->findOne();
$id = "55711efed0103b1598140076";
$realmongoid = new MongoId($id);
$arrayWithDriverInfo = array("filledBy" => $_SESSION['username']);
$collection->update(
array( '_id' => $realmongoid ),
array( '$set' => $arrayWithDriverInfo )
);
$obj = $collection->findOne();
var_dump($obj);
?>