这篇文章主要介绍了php实现的mongodb操作类,较为详细的分析了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 = "<?php/nreturn ".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://";
&nbs
大型站长资讯类网站!