更新MongoDB文档不起作用


Updating MongoDB Document Not Working

我只是想为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...

这是怎么回事?

尝试在更新中使用$set运算符,如下所示

<?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);
?>