Karp

PHP7 Mongo 第三方类的使用及demo
原创,转载请注明出处来自 奔赴de博客相信我, 你要面对现实, 现实就是好用的mongo 扩展 再php7.0 以...
扫描右侧二维码阅读全文
07
2017/03

PHP7 Mongo 第三方类的使用及demo

原创,转载请注明出处来自 奔赴de博客

相信我, 你要面对现实, 现实就是好用的mongo 扩展 再php7.0 以后不再支持了 ...
默哀一下, 虽然我并没有用过.

那么为了以后的开发我们必须使用 php的另一个 mongodb 扩展.
看看名字差不多吧. 但是用起来实在是在困难了.
先看下官方文档支持情况:

没错全是英文 方法非常不好用 ...

没办法 不好用也给用 , 但是要不说php 是世界上最好的语言呢!!! 有位国外的phper自己封装了一套方法,
和原来的语法差不多. 并且composer 也进行了收录. 现在直接就可以安装mongodb 的第三方类库了.

开发过程中选型php7 + mongo 我就再找合适的框架, 后台发现CI 和 ThinkPHP 都有 mongo支持类库,
BUT 都过期了. 不能有效支持mongodb 扩展...

在我不断百度下终于找到了合适的类库.
下面的连接就是 composer 下载下来的包, 我直接保存到服务器上, 提供下载.

Mongod扩展 PHP第三方类库 下载

但是毕竟是外国哥们写的代码注释什么的, 都很难理解, 我就自己写了个demo ,有兴趣可以参考下:

<?php
/*
 * mongodb 扩展 php类库使用demo
 * @author libenfu
 * @mail: i@91coder.org
 * 测试composer 中mongodb 方法准确性
 */

namespace Home\Controller;
use Think\Controller;

class TestMgController extends Controller {

    public $collection;

    public function __construct() {

        #建立连接  mongodb://username:password@host:port
        try {
            $manager = new \MongoDB\Driver\Manager("mongodb://username:password@host:port");
        } catch(Exception $e) {
            E($e->getMessage());
        }
       #下行代码为第三方类库
        $this->collection = new \MongoDB\Collection($manager, "test", "test");
    }

    public function findOne() {

        #读取一条数据
        $data = $this->collection->findOne(array('id' => 1));
        var_dump($data);
    }

    public function find() {

        #查找条件
        $where = array(
            'id' => 1
        );
        # 读取多条数据
        $options = array(
            'projection' => array('id' => 1, 'age' => 1, 'name' => -1), // 指定返回哪些字段 1 表示返回 -1 表示不返回
            'sort' => array('id' => -1), // 指定排序字段
            'limit' => 10, // 指定返回的条数
            'skip' => 0, // 指定起始位置
        );
        
        $cursor = $this->collection->find($where, $options);

        #find 查询需要用toArray() 转换 不能直接读取
        $dataList = $cursor->toArray();
        
        var_dump($dataList);
    }
    
    public function count() {

        #根据条件查找 id 大于 1 的文档数量
        $where = array(
            'id' => ['$gt' => 1]
        );
        $num = $this->collection->count($where);
        var_dump($num);
    }

    //去重查询
    public function distinct() {

        #参数1 字段, 参数2 查询条件
        $fileName = 'name';
        $where = array(
            'id' => ['$lt' => 100]
        );
        $ret = $this->collection->distinct($fileName, $where);
        var_dump($ret);
    }

    public function insertOne() {

        # 插入一条数据
        $data = array(
            'id' => 2,
            'age' => 20,
            'name' => '张三'
        );
        $ret = $this->collection->insertOne($data);
        var_dump($ret->getInsertedCount());
    }

    public function insertMany() {
    
        # 批量插入
        $data = array(
            ['id' => 1, 'age' => 21, 'name' => '1xiaoli'],
            ['id' => 2, 'age' => 22, 'name' => '2xiaoli'],
            ['id' => 3, 'age' => 23, 'name' => '3xiaoli'],
            ['id' => 4, 'age' => 26, 'name' => '4xiaoli'],
            ['id' => 5, 'age' => 24, 'name' => '5xiaoli'],
            ['id' => 6, 'age' => 25, 'name' => '6xiaoli'],
        );
        $ret = $this->collection->insertMany($data);
        # 返回插入数量
        var_dump($ret->getInsertedCount());
    }

    public function updateOne() {

        # updateOne 参数 1 查询条件, 参数2 要更换的字段
        $ret = $this->collection->updateOne(array('id' => 2), array('$set' => array('age' => 56)));
        # 返回更新的数量
        var_dump($ret->getMatchedCount());
    }

    #更新多条数据 和 更新一条数据基本一致只是影响的范围不同
    public function updateMany() {

        # updateMany 参数1 查询条件 ,参数2 要更换的字段
        $ret = $this->collection->updateMany(array('id' => ['$gt' => 1]), array('$set' => array('age' => 56, 'name' => 'x')));
        
        var_dump($ret->getMatchedCount());

    }

    public function deleteOne() {

        # deleteOne 参数 1 查询语句 , 参数2 
        $ret = $this->collection->deleteOne(array('id' => 2));
        var_dump($ret);
    }

    #删除多条数据
    public function  deleteMany() {

        # deleteMany 参数
        $ret = $this->collection->deleteMany(array('id' => array('$in' => array(1, 2))));
        var_dump($ret);
    }
    
    public function listIndexes() {

        //返回索引
        $options = array(
        );
        $ret = $this->collection->listIndexes($options);
        var_dump($ret);
    }
}

实际上Mongodb第三类库中提供了demo 上面仅是最基础的增删改查 , 希望能够帮助需要的人

最后 提供下 PHP mongodb 扩展下载地址 : http://pecl.php.net/package/mongodb

最后修改:2021 年 04 月 08 日 02 : 39 PM

3 条评论

  1. lee

    图片看不到啊,还是没给出第三方类库的名字啊。

  2. 误差

    下载不了第三方类库 能直接给名字吗

    1. LEN
      @误差

      不好意思 最近比较忙才看到 这个第三方类库 已经可以下载了

发表评论