原生PHP7操作mongodb4

Mongodb安装篇(CentOS7,Mongodb4.0.1):
https://blog.csdn.net/gaokcl/article/details/83587077

Mongodb 配置用户:
https://blog.csdn.net/gaokcl/article/details/101696511

mongodb版本 :mongodb4.0.1 
PHP版本 :php7.2.14
<?php
/**
 * 原生PHP操作mongodb
 * mongodb4.0.1 + php7.2.14
 */
class NativeMongoDB
{
    protected $mongo = null;

    public function __construct()
    {
        if (!isset($this->mongo) || empty($this->mongo)) {
            // $this->mongo = new MongoDB\Driver\Manager(getenv('MONGODB_LINK'));
            // 配置用户名密码的
            $this->mongo = new MongoDB\Driver\Manager('mongodb://username:password@ip:27017');
            // 未设置用户名与密码(不建议使用)
            $this->mongo = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
        }
    }

    //----------------------------------------------------------------
    // @ 插入操作,不用事前创建集合
    //----------------------------------------------------------------
    public function insert_mongo($data = [])
    {
        // 实例化一个添加类
        $bulk = new MongoDB\Driver\BulkWrite;

        // 添加你要添加的数据 可以是对象也可以是数组
        if (!isset($data) || empty($data)) { // todo 测试实例
            $bulk->insert(
                [
                    'user_id' => 1,
                    'name' => '小明',
                    'age' => '23',
                    'pass' => '123456',
                    'email' => '123456@qq.com',
                    'create_time' => time(),
                    'status' => '9'
                ]
            );
        }

        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); // 可选,修改确认

        // 第一个参数的格式 [ 数据库.集合名 ]
        $result = $this->mongo->executeBulkWrite('test.test', $bulk, $writeConcern);
        return (bool)$result;
    }

    //----------------------------------------------------------------
    // @ 查询--mongodb的数据查询
    //----------------------------------------------------------------

    /**
     * 查询--mongodb的数据查询
     * @param array $data
     * @param int $skip
     * @param int $limit
     * @param array $sort
     */
    public function select_mongo($data = [], $skip = 0, $limit = 10, $sort = ['age' => 1])
    {
        if (empty($data) || !isset($data)) {
            // 1,查询条件
            $data = [
                'email' => '123456@qq.com',
                // $gt`这是查询选择器,表示大于
                'age' => ['$gt' => '18']
            ];

            // 2,选项意义为:`skip`跳过0条数据,`limit`查询10条数据,`sort`按age字段正序排序(1为正序,-1为倒序)
            $aSort = [
                ['skip' => $skip, 'limit' => $limit, 'sort' => $sort]
            ];
        }

        // todo 查询
        $query = new MongoDB\Driver\Query($data, $aSort);
        $cursor = $this->mongo->executeQuery('test.test', $query);

        $result = [];
        if ($cursor) {
            foreach ($cursor as $doc) {
                $result[] = $doc;
            }
            $result = $this->object_array($result);
        }
        return $result;
    }

    //----------------------------------------------------------------
    // @ 修改--mongodb的数据修改
    //----------------------------------------------------------------

    public function update_mongo($data = [])
    {
        if (!isset($data) || empty($data)) {
            $bulk = new MongoDB\Driver\BulkWrite;
            $bulk->update(
                ['name' => '小缓'],
                [
                    // 注意 (int)18 与 (string)'18'
                    '$set' => ['age' => '18', 'status' => '9']
                ]);
        }

        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

        $res = $this->mongo->executeBulkWrite('test.test', $bulk, $writeConcern);
        return (bool)$res;
    }

    //----------------------------------------------------------------
    // @ 删除--mongodb的数据删除
    //----------------------------------------------------------------

    public function delete_mongo($data = [])
    {
        if (empty($data) || !isset($data)) {
            $data = ['name' => '小名', 'age' => "12"];
        }
        $bulk = new MongoDB\Driver\BulkWrite;
        // 删除条件
        $bulk->delete($data);

        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
        $res = $this->mongo->executeBulkWrite('test.test', $bulk, $writeConcern);
        return (bool)$res;
    }
    /**************************** 工具 ****************************/

    // PHP 对象 转为 数组
    public function object_array($array)
    {
        if (is_object($array)) {
            $array = (array)$array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
}


$model = new NativeMongoDB();
$c = $model->insert_mongo();
//$c = $model->select_mongo();
//$c = $model->update_mongo();
//$c = $model->delete_mongo();
var_dump($c);
exit;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值