PHP7 mongoDB扩展使用

栏目: PHP · 发布时间: 6年前

内容简介:最近在做的项目需要将PHP5.6升级到PHP7.0,使用过PHP-mongo扩展的同学应该知道,PHP7.0的mongodb扩展是完全不兼容PHP5.6的mongo扩展的,php-mongodb改如何使用呢。下面直接说明各种方法的使用:

最近在做的项目需要将 PHP 5.6升级到PHP7.0,使用过PHP-mongo扩展的同学应该知道,PHP7.0的 mongodb 扩展是完全不兼容PHP5.6的mongo扩展的,php-mongodb改如何使用呢。

下面直接说明各种方法的使用:

1.mongodb连接:

private function connect($confArr) {
    try{
        $connStr = "mongodb://" . $confArr['host'] . ":" . $confArr['port'] . "/" . $confArr['db_name'];
        $options = array(
            'username' => $confArr['username'],
            'password' => $confArr['password'],
            'readPreference' => $confArr['read_preference'],
            'connectTimeoutMS' => intval($confArr['connect_timeout_ms']),
            'socketTimeoutMS' => intval($confArr['socket_timeout_ms']),
        );
        $mc = new MongoDB\Driver\Manager($connStr, $options);
        return $mc;
    }
    catch(Exception $e){
        return false;
    }
}

2.查询find:

public function find($query = array(), $fields = array(), $collection, $sort = array(), $limit = 0, $skip = 0) {
    $conn = $this->connect();
    if (empty($conn)) {
        return false;
    }
    try {
        $data = array();
        $options = array();
        if (!empty($query)) {
            $options['projection'] = array_fill_keys($fields, 1);
        }
        if (!empty($sort)) {
            $options['sort'] = $sort;
        }
        if (!empty($limit)) {
            $options['skip'] = $skip;
            $options['limit'] = $limit;
        }
        $mongoQuery = new MongoDB\Driver\Query($query, $options);
        $readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
        $cursor = $conn->executeQuery($collection, $mongoQuery, $readPreference);
        foreach($cursor as $value) {
            $data[] = (array)$value;
        }
        return $data;
    } catch (Exception $e) {
        //记录错误日志
    }
    return false;
}

3.插入操作insert:

public function insert($addArr, $collection) {
    if (empty($addArr) || !is_array($addArr)) {
        return false;
    }
    $conn = $this->connect();
    if (empty($conn)) {
        return false;
    }
    try {
        $bulk = new MongoDB\Driver\BulkWrite();
        $bulk->insert($addArr);
        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 6000);
        $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
        if ($result->getInsertedCount()) {
            return true;
        }
    } catch (Exception $e) {
        //记录错误日志
    }
    return false;
}

4.删除delete:

public function delete($whereArr, $options = array(), $collection) {
    if (empty($whereArr)) {
        return false;
    }
    if (!isset($options['justOne'])) {
        $options = array(
            'justOne' => false,
        );
    }
    $conn = $this->connect();
    if (empty($conn)) {
        return false;
    }
    try {
        $bulk = new MongoDB\Driver\BulkWrite();
        $bulk->delete($whereArr, $options);
        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 30000);
        $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
        return true;
    } catch (Exception $e) {
        //记录错误日志
    }
    return false;
}

5.执行command操作:

private function command($params, $dbName) {
    $conn = $this->connect();
    if (empty($conn)) {
        return false;
    }
    try {
        $cmd = new MongoDB\Driver\Command($params);
        $result = $conn->executeCommand($dbName, $cmd);
        return $result;
    } catch (Exception $e) {
        //记录错误
    }
    return false;
}

6.统计count:

public function count($query, $collection) {
    try {
        $cmd = array(
            'count' => $collection,
            'query' => $query,
        );
        $res = $this->command($cmd);
        $result = $res->toArray();
        return $result[0]->n;
    } catch (Exception $e) {
        //记录错误
    }
    return false;
}

7.聚合distinct:

public function distinct($key, $where, $collection) {
    try {
        $cmd = array(
            'distinct' => $collection,
            'key' => $key,
            'query' => $where,
        );
        $res = $this->command($cmd);
        $result = $res->toArray();
        return $result[0]->values;
    } catch (Exception $e) {
        //记录错误
    }
    return false;
}

8.aggregate操作:

public function aggregate($where, $group, $collection) {
    try {
        $cmd = array(
            'aggregate' => $collection,
            'pipeline' => array(
                array(
                    '$match' => $where,
                ),
                array(
                    '$group' => $group,
                ),
            ),
            'explain' => false,
        );
        $res = $this->command($cmd);
        if (!$res) {
            return false;
        }
        $result = $res->toArray();
        return $result[0]->total;
    } catch (Exception $e) {
        //记录错误
    }
    return false;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创新者

创新者

[美] 沃尔特 · 艾萨克森 / 关嘉伟、牛小婧 / 中信出版社 / 2016-6 / 88.00

讲述了计算机和互联网从无到有的发展历程,并为我们生动地刻画出数字时代的创新者群像。 在近200年的数字化进程中群星闪耀,艾萨克森从一个计算机程序的创造者、诗人拜伦之女埃达说起,细数了这一群站在科学与人文交叉路口的创新者,他们包括通用型电子计算机的创造者奠奇利、科学家冯·诺依曼、仙童半导体公司的“八叛逆”、天才图灵、英特尔的格鲁夫、微软的比尔·盖茨、苹果公司的乔布斯、谷歌的拉里·佩奇等。《创新......一起来看看 《创新者》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具