簡體   English   中英

更新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);

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM