加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php封装的mongodb操作类代码

发布时间:2021-02-25 16:12:55 所属栏目:PHP教程 来源:网络整理
导读:核心代码 /* To change this template,choose Tools | Templates and open the template in the editor. */ class mongo_db { private $config; private $connection; private $db; private $connection_string; private $host; private $port; private $use
副标题[/!--empirenews.page--]

核心代码

/*

  • To change this template,choose Tools | Templates
  • and open the template in the editor.
    */

class mongo_db {

private $config;
private $connection;
private $db;
private $connection_string;
private $host;
private $port;
private $user;
private $pass;
private $dbname;
private $persist;
private $persist_key;
private $selects = array();
private $wheres = array();
private $sorts = array();
private $limit = 999999;
private $offset = 0;
private $timeout = 200;
private $key = 0;
/* -------------------------------------------------------------------------------- CONSTRUCTOR -------------------------------------------------------------------------------- Automatically check if the Mongo PECL extension has been installed/enabled. Generate the connection string and establish a connection to the MongoDB. /

public function __construct() {
if((IS_NOSQL != 1)){
return;
}
if (!class_exists('Mongo')) {
//$this->error("The MongoDB PECL extension has not been installed or enabled",500);
}
$configs =wxcity_base::load_config("cache","mongo_db");
$num = count($configs['connect']);
$this->timeout = trim($configs['timeout']);
$keys = wxcity_base::load_config('double');
$this->key = $keys['mongo_db'];
$this->config = $configs['connect'][$this->key];
$status = $this->connect();
if($status == false)
{
for($i = 1; $i < $num; $i++)
{
$n = $this->key + $i;
$key = $n >= $num ? $n - $num : $n;
$this->config = $configs['connect'][$key];
$status = $this->connect();
if($status!=false)
{
$keys['mongo_db'] = $key ;
$this->key = $key;
$data = "<?phpnreturn ".var_export($keys,true).";n?>";
file_put_contents(WHTY_PATH.'configs/double.php',$data,LOCK_EX);
break;
}
}
}
if($status==false)
{
die('mongoDB not connect');
}
}

function __destruct() {
if((IS_NOSQL != 1)){
return;
}
if($this->connection)
{
$this->connection->close();
}
}

/* -------------------------------------------------------------------------------- CONNECT TO MONGODB -------------------------------------------------------------------------------- Establish a connection to MongoDB using the connection string generated in the connection_string() method. If 'mongo_persist_key' was set to true in the config file,establish a persistent connection. We allow for only the 'persist' option to be set because we want to establish a connection immediately. /
private function connect() {
$this->connection_string();
$options = array('connect'=>true,'timeout'=>$this->timeout);
try {
$this->connection = new Mongo($this->connection_string,$options);
$this->db = $this->connection->{$this->dbname};
return($this);
} catch (MongoConnectionException $e) {
return false;
}

}
/* -------------------------------------------------------------------------------- BUILD CONNECTION STRING -------------------------------------------------------------------------------- Build the connection string from the config file. */
private function connection_string() {
$this->host = trim($this->config['hostname']);
$this->port = trim($this->config['port']);
$this->user = trim($this->config['username']);
$this->pass = trim($this->config['password']);
$this->dbname = trim($this->config['database']);
$this->persist = trim($this->config['autoconnect']);
$this->persist_key = trim($this->config['mongo_persist_key']);

$connection_string = "mongodb://";
if (emptyempty($this->host)) {
  $this->error("The Host must be set to connect to MongoDB",500);
} if (emptyempty($this->dbname)) {
  $this->error("The Database must be set to connect to MongoDB",500);
} if (!emptyempty($this->user) && !emptyempty($this->pass)) {
  $connection_string .= "{$this->user}:{$this->pass}@";
} if (isset($this->port) && !emptyempty($this->port)) {
  $connection_string .= "{$this->host}:{$this->port}";
} else {
  $connection_string .= "{$this->host}";
} $this->connection_string = trim($connection_string);

}

/* -------------------------------------------------------------------------------- Switch_db -------------------------------------------------------------------------------- Switch from default database to a different db */
public function switch_db($database = '') {
if (emptyempty($database)) {
$this->error("To switch MongoDB databases,a new database name must be specified",500);
} $this->dbname = $database;
try {
$this->db = $this->connection->{$this->dbname};
return(TRUE);
} catch (Exception $e) {
$this->error("Unable to switch Mongo Databases: {$e->getMessage()}",500);
}
}

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读