2015-06-25 81 views
1

我只是試圖更新文檔中的一個特定字段,我有一個MongoDB數據庫。更新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

嘗試使用$set運營商在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); 

?>