php7 mongodb操作类
很好用的php7操作mongodb类
https://blog.csdn.net/color_wind/article/details/52008674
<?php namespace ZXUN\Data\DataBase; final class MongoDB { private static $ins = []; private static $def = "default"; private $_conn = null; private static $_config = [ "default" => ["url" => "mongodb://127.0.0.1:27017", "dbName" => "windows_hr"], "db1" => ["url" => "mongodb://username:password@localhost:27017", "dbName" => "myDb1"], ]; private $_db = null; /** * 创建实例 * @param string $confKey * @return \MongoDB */ static function getInstance($confKey = NULL) { if (!$confKey) { $confKey = self::$def; } if (!isset(self::$ins[$confKey]) && ($conf = self::$_config[$confKey])) { $m = new MongoDB($conf); self::$ins[$confKey] = $m; } return self::$ins[$confKey]; } /** * 构造方法-单例模式 * MongoDB constructor. * @param array $conf */ private function __construct(array $conf) { $this->_conn = new \MongoDB\Driver\Manager($conf["url"] . "/{$conf["dbName"]}"); $this->_db = $conf["dbName"]; } /** * 插入数据 * @param string $collName * @param array $documents [["name"=>"values", ...], ...] * @param array $writeOps ["ordered"=>boolean,"writeConcern"=>array] * @return \MongoDB\Driver\Cursor */ function insert($collName, array $documents, array $writeOps = []) { $cmd = [ "insert" => $collName, "documents" => $documents, ]; $cmd += $writeOps; return $this->command($cmd); } /** * 删除数据 * @param string $collName * @param array $deletes [["q"=>query,"limit"=>int], ...] * @param array $writeOps ["ordered"=>boolean,"writeConcern"=>array] * @return \MongoDB\Driver\Cursor */ function del($collName, array $deletes, array $writeOps = []) { foreach ($deletes as &$_) { if (isset($_["q"]) && !$_["q"]) { $_["q"] = (Object)[]; } if (isset($_["limit"]) && !$_["limit"]) { $_["limit"] = 0; } } $cmd = [ "delete" => $collName, "deletes" => $deletes, ]; $cmd += $writeOps; return $this->command($cmd); } /** * 更新数据 * @param string $collName * @param array $updates [["q"=>query,"u"=>update,"upSet"=>boolean,"multi"=>boolean], ...] * @param array $writeOps ["ordered"=>boolean,"writeConcern"=>array] * @return \MongoDB\Driver\Cursor */ function update($collName, array $updates, array $writeOps = []) { $cmd = [ "update" => $collName, "updates" => $updates, ]; $cmd += $writeOps; return $this->command($cmd); } /** * 查询 * @param string $collName * @param array $filter [query] * @param array $writeOps * @return \MongoDB\Driver\Cursor */ function query($collName, array $filter, array $writeOps = []) { $cmd = [ "find" => $collName, "filter" => $filter ]; $cmd += $writeOps; return $this->command($cmd); } /** * 执行MongoDB命令 * @param array $param * @return \MongoDB\Driver\Cursor */ function command(array $param) { $cmd = new \MongoDB\Driver\Command($param); return $this->_conn->executeCommand($this->_db, $cmd); } /** * 获取当前mongoDB Manager * @return \MongoDB\Driver\Manager */ function getMongoManager() { return $this->_conn; } }
使用方法
<?php require_once 'm_mgdb.php'; // 示例代码 //$db = m_mgdb::i("mdb1"); // 使用配置self::$_config["mdb1"] $db = m_mgdb::i(); // 使用配置self::$_config[self::$def] $collname = "proinfo"; // echo "\n---------- 查询支持命令 -----------\n"; // $cmd = [ // "listCommands" => 1, // ]; // $rs = $db->command($cmd); // print_r($rs->toArray()); echo "\n---------- 删除 proinfo 所有数据 -----------\n"; $delets = [ ["q" => [],"limit" => 0] ]; $rs = $db->del($collname, $delets); print_r($rs->toArray()); echo "\n---------- 创建索引 -----------\n"; $cmd = [ "createIndexes" => $collname, "indexes" => [ ["name" => "proname_idx", "key" => ["name"=>1],"unique" => true], ], ]; $rs = $db->command($cmd); print_r($rs->toArray()); echo "\n---------- 查询索引 -----------\n"; $cmd = [ "listIndexes" => $collname, ]; $rs = $db->command($cmd); print_r($rs->toArray()); echo "\n------------ 插入数据 ---------\n"; $rows = [ ["name" => "ns w1","type"=>"ns","size"=>["height"=>150,"width"=>30],"price"=>3000], ["name" => "ns hd","type"=>"ns","size"=>["height"=>154,"width"=>30],"price"=>3500], ["name" => "ns w3","type"=>"ns","size"=>["height"=>160,"width"=>30],"price"=>3800], ["name" => "bt s1","type"=>"bt","size"=>["height"=>158,"width"=>32],"price"=>3500], ["name" => "bt w1","type"=>"bt","size"=>["height"=>157,"width"=>30],"price"=>3600], ["name" => "an w1","type"=>"bt","size"=>["height"=>157,"width"=>30],"price"=>3700], ["name" => "wn w6","type"=>"wn","size"=>["height"=>157,"width"=>30],"price"=>3500], ]; $rs = $db->insert($collname, $rows); print_r($rs->toArray()); echo "\n---------- 查询数据 -----------\n"; $filter = [ "name" => ['$regex' => '\sw\d'], // mongo 正则匹配 '$or' => [["type" => "bt"], ["size.height" => ['$gte' => 160]]] ]; $queryWriteOps = [ "projection" => ["_id" => 0], "sort" => ["price" => -1], "limit" => 20 ]; $rs = $db->query($collname, $filter, $queryWriteOps); print_r($rs->toArray()); echo "\n---------- 更新数据 -----------\n"; $updates = [ [ "q" => ["name" => "ns w3"], "u" => ['$set' => ["size.height" => 140],'$inc' => ["size.width" => 14]], "multi" => true, ] ]; $rs = $db->update($collname, $updates); print_r($rs->toArray()); echo "\n---------- 查询数据 -----------\n"; $filter = [ "name" => "ns w3", ]; $rs = $db->query($collname, $filter, $queryWriteOps); print_r($rs->toArray());
赞:(0)
踩:(0)
- 相关文章
- php7中使用mongodb的aggregate进行
- php7 mongodb distinct及count方法
- mongodb基础知识
- mongodb报No suitable servers fou
- php操作mongodb
- mongodb学习网址及基础教程
- mongodb数据库添加及删除
- mongodb集合创建与删除
- mongodb文档添加与更新
- mongodb文档查询
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- laravel页面静态化的方法
- centos7.4 64位下swoole安装及配置
- navicate连接mycat报1184错误
- curl设置超时不起作用(CURLOPT_TIM
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:623篇
- 浏览数:1357698