MongoDB更新:如何检查更新是成功还是失败?

zmg*_*zmg 9 php mongodb

我正在使用pecl驱动程序在PHP中使用MongoDB.我的更新工作得很好,但我想在我的函数中构建一些错误检查.

我尝试在一个非常简单的函数中使用lastError():

function system_db_update_object($query, $values, $database, $collection) {
    $connection = new Mongo();
    $collection = $connection->$database->$collection;
    $connection->$database->resetError(); //Added for debugging
    $collection->update(
        $query,
        array('$set' => $values));
    //$errorArray = $connection->$database->lastError();
    var_dump($connection->$database->lastError());exit; // Var dump and /Exit/
}
Run Code Online (Sandbox Code Playgroud)

但几乎不管我尝试更新(无论是否存在),我都得到了相同的基本结果:

array(4) {
  ["err"]=>
  NULL
  ["updatedExisting"]=>
  bool(true)
  ["n"]=>
  float(1)
  ["ok"]=>
  float(1)
}
Run Code Online (Sandbox Code Playgroud)

我如何知道更新是成功还是失败?

mdi*_*olf 4

“n”字段是已更新的文档数量,“updatedExisting”表明是否有任何文档已更新。您应该能够检查这些字段以查看更新是否成功。