【问题标题】:mongodb getLastError() phpmongodb getLastError() php
【发布时间】:2012-11-28 22:35:27
【问题描述】:

如何在 php 中使用 getLastError() 来检查我的保存方法是否插入到 mongo?

我的数据库设置如下:

$this->databaseEngine = $app['mongo'];
$this->db = $this->databaseEngine->dbname;
$this->collection = $this->db->collectionname;

我的插入查询如下所示:

$query = array(
    'fieldname' => $fieldname
);
$this->collection->insert($query);

然后我想使用 getLastError() 来检查它是否正确插入,如果不正确,为什么。但我不确定如何实现它。

我在插入后使用吗:

$this->collection->getLastError("what goes here?");

干杯。

更新

我最终用这个来得到最后一个错误:

echo '<pre>' . print_r($this->databaseEngine->lastError(), true) . '</pre>';

Sammaye 的方法同样有效,见下文。

【问题讨论】:

    标签: php mongodb getlasterror


    【解决方案1】:

    $this->collection->getLastError("这里发生了什么?");

    那里什么都没有,getLastError 的返回是来自 MongoDB (http://www.php.net/manual/en/mongodb.lasterror.php) 的最后一个错误。它也用于 MongoDB 类 (atm)。

    您不必那样使用它,而是可以这样做:

    $this->collection->insert($query, ARRAY('safe' => TRUE));
    

    这将从函数返回一个数组,详细说明它是否实际插入。该数组的详细信息可以通过阅读此页面找到:

    http://www.php.net/manual/en/mongocollection.insert.php

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2018-01-07
      • 2013-07-28
      • 2013-08-15
      相关资源
      最近更新 更多