doujiang1939 2016-08-31 07:16
浏览 95
已采纳

如何在php中获取MongoDB更新/删除操作的状态?

I am using this

function delete($col,$condition = array()){
    $bulk = new MongoDB\Driver\BulkWrite;
    $bulk->delete($condition, ['limit' => 1]);

    $manager = new MongoDB\Driver\Manager($this->mongo);
    $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
    $result = $manager->executeBulkWrite($this->db.'.'.$col, $bulk, $writeConcern);
    var_dump($result);

}

And the code works and getting output like bellow

object(MongoDB\Driver\WriteResult)#30 (9) { ["nInserted"]=> int(0) ["nMatched"]=> int(0) ["nModified"]=> int(0) ["nRemoved"]=> int(0) ["nUpserted"]=> int(0) ["upsertedIds"]=> array(0) { } ["writeErrors"]=> array(0) { } ["writeConcernError"]=> NULL ["writeConcern"]=> array(4) { ["w"]=> string(8) "majority" ["wmajority"]=> bool(true) ["wtimeout"]=> int(1000) ["journal"]=> NULL } }

Now I can't convert the object to array. How to get the properties like "nRemoved" / "nUpdated" from the object?

  • 写回答

1条回答 默认 最新

  • douyi1197 2017-02-17 10:29
    关注

    You can do it like this:

    $bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]);
    $bulk->update(
        $condition,
        $data,
        $n_opts
    );
    if($result = $this->mongo->executeBulkWrite($this->db.'.'.$collection, $bulk)){
        if(($result->getMatchedCount() > 0 AND $result->getModifiedCount() > 0) OR ($result->getMatchedCount() == 0 AND $result->getUpsertedCount() > 0)){
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
    

    To return deletes count:

    $result->getDeletedCount();

    More info about this methods here: http://php.net/manual/en/class.mongodb-driver-writeresult.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?