2015-06-25 82 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); 

?>