php封装的封装的mongodb操作类代码操作类代码
主要分享一个php封装的mongodb操作类,有需要的朋友们可以参考一下
核心代码
<?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 $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 = "<?phpreturn ".var_export($keys, true).";?>";
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};