ホームページ  >  記事  >  バックエンド開発  >  mongodb カプセル化のいくつかの方法を共有する

mongodb カプセル化のいくつかの方法を共有する

不言
不言オリジナル
2018-04-18 10:35:142108ブラウズ

この記事の内容は、mongodb のカプセル化のいくつかの方法を共有することです。これは、必要な友人に参照していただくために共有します。これは php5.6 用に書かれたものです。上記はそのような実装ではありません。php7mongodb のカプセル化クラスをお持ちの場合は、リンクを送っていただければ確認させていただきます

<?php/**
 * Created by sublime.
 * User: yuepeng
 * Date: 2017/12/1
 * Time: 15:44
 */namespace Lib;use MongoClient;//下面是驱动类,现在没有用到,我的版本php5.5,如果是php7就要用到这些类use MongoDB\BSON\ObjectID;use MongoDB\Driver\BulkWrite;use MongoDB\Driver\Command;use MongoDB\Driver\Cursor;use MongoDB\Driver\Exception\AuthenticationException;use MongoDB\Driver\Exception\BulkWriteException;use MongoDB\Driver\Exception\ConnectionException;use MongoDB\Driver\Exception\InvalidArgumentException;use MongoDB\Driver\Exception\RuntimeException;use MongoDB\Driver\Manager;use MongoDB\Driver\Query as MongoQuery;use MongoDB\Driver\ReadPreference;use MongoDB\Driver\WriteConcern;/**
 * PHP操作mongodb数据库操作类
 */class MongoDb {
    protected $database    = &#39;&#39;;    protected $mo;    /**
     * 构造方法
     */
    public function __construct() {
        $database = DBNAME;        $mongo = $this->getConnect(DBSERVER, DBUSER, DBPASS, DBPORT);        $this->database = $mongo->$database;
    }    /**
     * 数据库单例方法
     * @param $server
     * @param $user
     * @param $password
     * @param $port
     * test 测试
     * @return Mongo
     */
    /*public function getConnect($server, $user, $password, $port)
    {
        // echo "mongodb://{$server}:{$port}";die;
        // $mongo = new MongoClient("mongodb://{$server}:{$port}");
        $mongo = new MongoClient("mongodb://{$server}:{$port}");
        return $mongo;
    }*/
    /**
     * 数据库单例方法
     * @param $server
     * @param $user
     * @param $password
     * @param $port
     * @return Mongo
     */
    public function getConnect($server, $user, $password, $port) {
        if (isset($this->mo)) {            return $this->mo;
        } else {            if (!empty($server)) {                if (!empty($port)) {                    if (!empty($user) && !empty($password)) {                        $this->mo = new MongoClient("mongodb://{$user}:{$password}@{$server}:{$port}");
                    } else {                        $this->mo = new MongoClient("mongodb://{$server}:{$port}");
                    }
                } else {                    $this->mo = new MongoClient("mongodb://{$server}");
                }
            } else {                $this->mo = new MongoClient();
            }            return $this->mo;
        }
    }    /**
     * 查询表中所有数据
     * @param $table
     * @param array $where
     * @param array $sort
     * @param string $limit
     * @param string $skip
     * @return array|int
     */
    public function getAll($table, $where = array(), $sort = array(), $limit = &#39;&#39;, $skip = &#39;&#39;) {
        if (!empty($where)) {            $data = $this->database->$table->find($where);
        } else {            $data = $this->database->$table->find();
        }        if (!empty($sort)) {            $data = $data->sort($sort);
        }        if (!empty($limit)) {            $data = $data->limit($limit);
        }        if (!empty($skip)) {            $data = $data->skip($skip);
        }        $newData = array();        while ($data->hasNext()) {            $newData[] = $data->getNext();
        }        if (count($newData) == 0) {            return 0;
        }        return $newData;
    }    /**
     * 查询表中所有数据,将_id 对象变成数组
     * @param $table
     * @param array $where
     * @param array $sort
     * @param string $limit
     * @param string $skip
     * @return array
     */
    public function getAllArray($table, $where = array(), $sort = array(), $limit = &#39;&#39;, $skip = &#39;&#39;) {
        if (!empty($where)) {            $data = $this->database->$table->find($where);
        } else {            $data = $this->database->$table->find();
        }        if (!empty($sort)) {            $data = $data->sort($sort);
        }        if (!empty($limit)) {            $data = $data->limit($limit);
        }        if (!empty($skip)) {            $data = $data->skip($skip);
        }        $newData = array();        while ($data->hasNext()) {            $newData[] = $data->getNext();
        }        if (count($newData) == 0) {            return 0;
        }        foreach ($newData as $key => $val) {            $id = $val[&#39;_id&#39;]->{&#39;$id&#39;};            $newData[$key][&#39;_id&#39;] = $id;
        }        return $newData;
    }    /**
     * 查询指定一条数据
     * @param $table
     * @param array $where
     * @return int
     */
    public function getOne($table, $where = array()) {
        if (!empty($where)) {            $data = $this->database->$table->findOne($where);
        } else {            $data = $this->database->$table->findOne();
        }        return $data;
    }    /**
     * 统计个数
     * @param $table
     * @param array $where
     * @return mixed
     */
    public function getCount($table, $where = array()) {
        if (!empty($where)) {            $data = $this->database->$table->find($where)->count();
        } else {            $data = $this->database->$table->find()->count();
        }        return $data;
    }    /**
     * 直接执行mongo命令
     * @param $sql
     * @return array
     */
    public function toExcute($sql) {
        $result = $this->database->execute($sql);        return $result;
    }    /**
     * 分组统计个数
     * @param $table
     * @param $where
     * @param $field
     */
    public function groupCount($table, $where, $field) {
        $cond = array(            array(                &#39;$match&#39; => $where,
            ),            array(                &#39;$group&#39; => array(                    &#39;_id&#39; => &#39;$&#39; . $field,                    &#39;count&#39; => array(&#39;$sum&#39; => 1),
                ),
            ),            array(                &#39;$sort&#39; => array("count" => -1),
            ),
        );        $this->database->$table->aggregate($cond);
    }    /**
     * 删除数据
     * @param $table
     * @param $where
     * @return array|bool
     */
    public function toDelete($table, $where) {
        $re = $this->database->$table->remove($where);        return $re;
    }    /**
     * 插入数据
     * @param $table
     * @param $data
     * @return array|bool
     */
    public function toInsert($table, $data) {
        $re = $this->database->$table->insert($data);        return $re;
    }    /**
     * 更新数据
     * @param $table
     * @param $where
     * @param $data
     * @return bool
     */
    public function toUpdate($table, $where, $data) {
        $re = $this->database->$table->update($where, array(&#39;$set&#39; => $data));        return $re;
    }    /**
     * 获取唯一数据
     * @param $table
     * @param $key
     * @return array
     */
    public function distinctData($table, $key, $query = array()) {
        if (!empty($query)) {            $where = array(&#39;distinct&#39; => $table, &#39;key&#39; => $key, &#39;query&#39; => $query);
        } else {            $where = array(&#39;distinct&#39; => $table, &#39;key&#39; => $key);
        }        $data = $this->database->command($where);        return $data[&#39;values&#39;];
    }
}?>

関連する推奨事項:

php-mongodb の拡張使用法

以上がmongodb カプセル化のいくつかの方法を共有するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。