更新 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 operator in your update 如下

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

?>