核心代码
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 = "";
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();
}
}
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;
}
}
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);
}
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);
}
}
public function select($includes = array(), $excludes = array()) {
if (!is_array($includes)) {
$includes = array();
}
if (!is_array($excludes)) {
$excludes = array();
}
if (!emptyempty($includes)) {
foreach ($includes as $col) {
$this->selects[$col] = 1;
}
} else {
foreach ($excludes as $col) {
$this->selects[$col] = 0;
}
} return($this);
}
public function where($wheres = array()) {
foreach ((array)$wheres as $wh => $val) {
$this->wheres[$wh] = $val;
} return($this);
}
public function where_in($field = "", $in = array()) {
$this->where_init($field);
$this->wheres[$field]['$in'] = $in;
return($this);
}
public function where_not_in($field = "", $in = array()) {
$this->where_init($field);
$this->wheres[$field]['$nin'] = $in;
return($this);
}
public function where_gt($field = "", $x) {
$this->where_init($field);
$this->wheres[$field]['$gt'] = $x;
return($this);
}
public function where_gte($field = "", $x) {
$this->where_init($field);
$this->wheres[$field]['$gte'] = $x;
return($this);
}
public function where_lt($field = "", $x) {
$this->where_init($field);
$this->wheres[$field]['$lt'] = $x;
return($this);
}
public function where_lte($field = "", $x) {
$this->where_init($field);
$this->wheres[$field]['$lte'] = $x;
return($this);
}
public function where_between($field = "", $x, $y) {
$this->where_init($field);
$this->wheres[$field]['$gte'] = $x;
$this->wheres[$field]['$lte'] = $y;
return($this);
}
public function where_between_ne($field = "", $x, $y) {
$this->where_init($field);
$this->wheres[$field]['$gt'] = $x;
$this->wheres[$field]['$lt'] = $y;
return($this);
}
public function where_ne($field = "", $x) {
$this->where_init($field);
$this->wheres[$field]['$ne'] = $x;
return($this);
}
public function where_or($field = "", $values) {
$this->where_init($field);
$this->wheres[$field]['$or'] = $values;
return($this);
}
public function where_and($elements_values = array()) {
foreach ((array)$elements_values as $element => $val) {
$this->wheres[$element] = $val;
} return($this);
}
public function where_mod($field, $num, $result) {
$this->where_init($field);
$this->wheres[$field]['$mod'] = array($num, $result);
return($this);
}
public function where_size($field = "", $size = "") {
$this->_where_init($field);
$this->wheres[$field]['$size'] = $size;
return ($this);
}
public function like($field = "", $value = "", $flags = "i", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE) {
$field = (string) trim($field);
$this->where_init($field);
$value = (string) trim($value);
$value = quotemeta($value);
if ($enable_start_wildcard !== TRUE) {
$value = "^" . $value;
} if ($enable_end_wildcard !== TRUE) {
$value .= "$";
} $regex = "/$value/$flags";
$this->wheres[$field] = new MongoRegex($regex);
return($this);
}
public function wheres($where){
$this->wheres = $where;
}
public function order_by($fields = array()) {
if (!is_array($fields) || !count($fields)) return ;
foreach ($fields as $col => $val) {
if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
$this->sorts[$col] = -1;
} else {
$this->sorts[$col] = 1;
}
} return($this);
}
public function limit($x = 99999) {
if ($x !== NULL && is_numeric($x) && $x >= 1) {
$this->limit = (int) $x;
} return($this);
}
public function offset($x = 0) {
if ($x !== NULL && is_numeric($x) && $x >= 1) {
$this->offset = (int) $x;
} return($this);
}
public function get_where($collection = "", $where = array(), $limit = 99999, $orderby=array()) {
if (is_array($orderby) || !emptyempty($orderby)) {
$order_by = $this->order_by($order_by);
}
return($this->where($where)->limit($limit)->get($collection));
}
public function selectA($collection = "", $limit = 99999, $orderby=array()) {
if(intval($limit)<1){
$limit = 999999;
}
$order_by = $this->order_by($orderby);
$re = $this->limit($limit)->get($collection);
$this->clear();
return (array)$re;
}
public function listinfo($collection = "", $orderby=array(), $page=1, $pagesize=12) {
$page = max(intval($page), 1);
$offset = $pagesize * ($page - 1);
$pagesizes = $offset + $pagesize;
$this->offset($offset);
$order_by = $this->order_by($orderby);
$re = $this->limit($pagesize)->get($collection);
$this->limit(999999);
$count = $this->count($collection);
$this->pages = pages($count, $page, $pagesize);
return (array)$re;
}
public function get($collection = "") {
if (emptyempty($collection)) {
$this->error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
} $results = array();
$documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit)->skip((int) $this->offset)->sort($this->sorts);
$returns = array();
foreach ($documents as $doc): $returns[] = $doc;
endforeach;
return($returns);
}
public function getMy($collection = "") {
if (emptyempty($collection)) {
$this->error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
} $results = array();
$documents = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int) $this->limit)->skip((int) $this->offset)->sort($this->sorts);
$returns = array();
foreach ($documents as $doc): $returns[] = $doc;
endforeach;
$this -> clear();
return($returns);
}
public function count($collection = "") {
if (emptyempty($collection)) {
$this->error("In order to retreive a count of documents from MongoDB, a collection name must be passed", 500);
} $count = $this->db->{$collection}->find($this->wheres)->limit((int) $this->limit)->skip((int) $this->offset)->count();
$this->clear();
return($count);
}
public function insert($collection = "", $data = array(), $name='ID') {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to insert into", 500);
} if (count($data) == 0 || !is_array($data)) {
$this->error("Nothing to insert into Mongo collection or insert is not an array", 500);
} try {
$re = put_sqs('list_mongo_creaseidsqs','1');
if(is_numeric($re)){
$re++;
$data[$name] = intval($re);
}else{
$data[$name] = intval(time());
//die('mongosqs error');
}
$this->db->{$collection}->insert($data, array('fsync' => TRUE));
$this->clear();
return $data[$name];
} catch (MongoCursorException $e) {
$this->error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
public function insertWithId($collection = "", $data = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to insert into", 500);
} if (count($data) == 0 || !is_array($data)) {
$this->error("Nothing to insert into Mongo collection or insert is not an array", 500);
} try {
$this->db->{$collection}->insert($data, array('fsync' => TRUE));
$this->clear();
return 1;
} catch (MongoCursorException $e) {
$this->error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
public function update($collection = "", $data = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to update", 500);
} if (count($data) == 0 || !is_array($data)) {
$this->error("Nothing to update in Mongo collection or update is not an array", 500);
} try {
$this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => TRUE, 'multiple' => FALSE));
$this->clear();
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Update of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
public function update_all($collection = "", $data = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to update", 500);
} if (count($data) == 0 || !is_array($data)) {
$this->error("Nothing to update in Mongo collection or update is not an array", 500);
} try {
$this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => TRUE, 'multiple' => TRUE));
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Update of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
public function delete($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to delete from", 500);
} try {
$this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => TRUE));
$this->clear();
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
public function delete_all($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection selected to delete from", 500);
} try {
$this->db->{$collection}->remove($this->wheres, array('fsync' => TRUE, 'justOne' => FALSE));
return(TRUE);
} catch (MongoCursorException $e) {
$this->error("Delete of data into MongoDB failed: {$e->getMessage()}", 500);
}
}
public function add_index($collection = "", $keys = array(), $options = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to add index to", 500);
} if (emptyempty($keys) || !is_array($keys)) {
$this->error("Index could not be created to MongoDB Collection because no keys were specified", 500);
} foreach ($keys as $col => $val) {
if ($val == -1 || $val === FALSE || strtolower($val) == 'desc') {
$keys[$col] = -1;
} else {
$keys[$col] = 1;
}
} if ($this->db->{$collection}->ensureIndex($keys, $options) == TRUE) {
$this->clear();
return($this);
} else {
$this->error("An error occured when trying to add an index to MongoDB Collection", 500);
}
}
public function remove_index($collection = "", $keys = array()) {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to remove index from", 500);
} if (emptyempty($keys) || !is_array($keys)) {
$this->error("Index could not be removed from MongoDB Collection because no keys were specified", 500);
} if ($this->db->{$collection}->deleteIndex($keys, $options) == TRUE) {
$this->clear();
return($this);
} else {
$this->error("An error occured when trying to remove an index from MongoDB Collection", 500);
}
}
public function remove_all_indexes($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to remove all indexes from", 500);
} $this->db->{$collection}->deleteIndexes();
$this->clear();
return($this);
}
public function list_indexes($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to remove all indexes from", 500);
} return($this->db->{$collection}->getIndexInfo());
}
public function drop_collection($collection = "") {
if (emptyempty($collection)) {
$this->error("No Mongo collection specified to drop from database", 500);
} $this->db->{$collection}->drop();
return TRUE;
}
private function clear() {
$this->selects = array();
$this->wheres = array();
$this->limit = NULL;
$this->offset = NULL;
$this->sorts = array();
}
private function where_init($param) {
if (!isset($this->wheres[$param])) {
$this->wheres[$param] = array();
}
}
public function error($str, $t) {
echo $str;
exit;
}
}
?>
使用范例:
$table_name=trim(strtolower($this->table_name));
$this->mongo_db->where($where);
$order=!emptyempty($order)?array('AID'=>'DESC'):array('AID'=>'ASC');//升序降序
$infos=$this->mongo_db->listinfo($table_name,$order,$page,$pagesize);
这篇文章就到这结束了,大家可以多学习一下



