您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

分享下mongodb封装的几个方法

2024/12/12 13:15:34发布28次查看
这篇文章介绍的内容是关于分享下mongodb封装的几个方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
这个是我写的php5.6的 ,但是php7以上就不是这样的实现了,大家要是有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 = ''; 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 = '', $skip = '') { 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 = '', $skip = '') { 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['_id']->{'$id'}; $newdata[$key]['_id'] = $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( '$match' => $where, ), array( '$group' => array( '_id' => '$' . $field, 'count' => array('$sum' => 1), ), ), array( '$sort' => 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('$set' => $data)); return $re; } /** * 获取唯一数据 * @param $table * @param $key * @return array */ public function distinctdata($table, $key, $query = array()) { if (!empty($query)) { $where = array('distinct' => $table, 'key' => $key, 'query' => $query); } else { $where = array('distinct' => $table, 'key' => $key); } $data = $this->database->command($where); return $data['values']; } }?>
相关推荐:
php-mongodb的扩展使用方法 
以上就是分享下mongodb封装的几个方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product