资讯专栏INFORMATION COLUMN

PHP7 mongoDB扩展使用

objc94 / 951人阅读

摘要:最近在做的项目需要将升级到,使用过扩展的同学应该知道,的扩展是完全不兼容的扩展的,改如何使用呢。

最近在做的项目需要将PHP5.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 MongoDBDriverManager($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 MongoDBDriverQuery($query, $options);
        $readPreference = new MongoDBDriverReadPreference(MongoDBDriverReadPreference::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 MongoDBDriverBulkWrite();
        $bulk->insert($addArr);
        $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::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 MongoDBDriverBulkWrite();
        $bulk->delete($whereArr, $options);
        $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::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 MongoDBDriverCommand($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;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/31366.html

相关文章

  • 升级PHP7 jenssegers/mongodb expected to be a referen

    摘要:上的扩展有两个,都是官方出品的,一个叫,一个是,前者已经被官方废弃,不再提供稳定的更新,官方推荐使用后者,并且后者是支持的。在上搜索即可找到,也可以通过安装。使用作为中间件,不用修改现有代码。 pecl上的mongodb扩展有两个,都是官方出品的,一个叫mongo,一个是mongodb,前者已经被官方废弃,不再提供稳定的更新,官方推荐使用后者,并且后者是支持php7的。在pecl上搜索...

    BicycleWarrior 评论0 收藏0
  • 升级PHP7 jenssegers/mongodb expected to be a referen

    摘要:上的扩展有两个,都是官方出品的,一个叫,一个是,前者已经被官方废弃,不再提供稳定的更新,官方推荐使用后者,并且后者是支持的。在上搜索即可找到,也可以通过安装。使用作为中间件,不用修改现有代码。 pecl上的mongodb扩展有两个,都是官方出品的,一个叫mongo,一个是mongodb,前者已经被官方废弃,不再提供稳定的更新,官方推荐使用后者,并且后者是支持php7的。在pecl上搜索...

    tangr206 评论0 收藏0
  • 升级PHP7操作MongoDB

    摘要:在及以前,官方提供了两个扩展,和,其中是对以等几个核心类为基础的类群进行操作,封装得很方便,所以基本上都会选择扩展。这种想法很违背简化操作带来的语法问题而专注逻辑优化的思路。 前言 使用 PHP+MongoDB 的用户很多,因为 MongoDB 对非结构化数据的存储很方便。在 PHP5 及以前,官方提供了两个扩展,Mongo 和 MongoDB,其中 Mongo 是对以 MongoCl...

    hizengzeng 评论0 收藏0
  • 升级PHP7操作MongoDB

    摘要:在及以前,官方提供了两个扩展,和,其中是对以等几个核心类为基础的类群进行操作,封装得很方便,所以基本上都会选择扩展。这种想法很违背简化操作带来的语法问题而专注逻辑优化的思路。 前言 使用 PHP+MongoDB 的用户很多,因为 MongoDB 对非结构化数据的存储很方便。在 PHP5 及以前,官方提供了两个扩展,Mongo 和 MongoDB,其中 Mongo 是对以 MongoCl...

    zlyBear 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<