php封装的mongodb操作类代码
核心代码 /*
class mongo_db { private $config; public function __construct() { function __destruct() { /* -------------------------------------------------------------------------------- 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. / }
} /* -------------------------------------------------------------------------------- Switch_db -------------------------------------------------------------------------------- Switch from default database to a different db */ /* -------------------------------------------------------------------------------- SELECT FIELDS -------------------------------------------------------------------------------- Determine which fields to include OR which to exclude during the query process. Currently,including and excluding at the same time is not available,so the $includes array will take precedence over the $excludes array. If you want to only choose fields to exclude,leave $includes an empty array(). @usage: $this->mongo_db->select(array('foo','bar'))->get('foobar'); / /* -------------------------------------------------------------------------------- WHERE PARAMETERS -------------------------------------------------------------------------------- Get the documents based on these search parameters. The $wheres array should be an associative array with the field as the key and the value as the search criteria. @usage = $this->mongo_db->where(array('foo' => 'bar'))->get('foobar'); */ /* -------------------------------------------------------------------------------- WHERE_IN PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is in a given $in array(). @usage = $this->mongo_db->where_in('foo',array('bar','zoo','blah'))->get('foobar'); */ /* -------------------------------------------------------------------------------- WHERE_NOT_IN PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is not in a given $in array(). @usage = $this->mongo_db->where_not_in('foo','blah'))->get('foobar'); */ /* -------------------------------------------------------------------------------- WHERE GREATER THAN PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is greater than $x @usage = $this->mongo_db->where_gt('foo',20); */ /* -------------------------------------------------------------------------------- WHERE GREATER THAN OR EQUAL TO PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is greater than or equal to $x @usage = $this->mongo_db->where_gte('foo',20); */ /* -------------------------------------------------------------------------------- WHERE LESS THAN PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is less than $x @usage = $this->mongo_db->where_lt('foo',20); */ /* -------------------------------------------------------------------------------- WHERE LESS THAN OR EQUAL TO PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is less than or equal to $x @usage = $this->mongo_db->where_lte('foo',20); */ /* -------------------------------------------------------------------------------- WHERE BETWEEN PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is between $x and $y @usage = $this->mongo_db->where_between('foo',20,30); */ /* -------------------------------------------------------------------------------- WHERE BETWEEN AND NOT EQUAL TO PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is between but not equal to $x and $y @usage = $this->mongo_db->where_between_ne('foo',30); */ /* -------------------------------------------------------------------------------- WHERE NOT EQUAL TO PARAMETERS -------------------------------------------------------------------------------- Get the documents where the value of a $field is not equal to $x @usage = $this->mongo_db->where_between('foo',30); */ /* -------------------------------------------------------------------------------- WHERE OR -------------------------------------------------------------------------------- Get the documents where the value of a $field is in one or more values @usage = $this->mongo_db->where_or('foo',array( 'foo','bar','blegh' ); */ /* -------------------------------------------------------------------------------- WHERE AND -------------------------------------------------------------------------------- Get the documents where the elements match the specified values @usage = $this->mongo_db->where_and( array ( 'foo' => 1,'b' => 'someexample' ); */ /* -------------------------------------------------------------------------------- WHERE MOD -------------------------------------------------------------------------------- Get the documents where $field % $mod = $result @usage = $this->mongo_db->where_mod( 'foo',10,1 ); */ /* -------------------------------------------------------------------------------- Where size -------------------------------------------------------------------------------- Get the documents where the size of a field is in a given $size int @usage : $this->mongo_db->where_size('foo',1)->get('foobar'); */ /* -------------------------------------------------------------------------------- LIKE PARAMETERS -------------------------------------------------------------------------------- Get the documents where the (string) value of a $field is like a value. The defaults allow for a case-insensitive search. @param $flags Allows for the typical regular expression flags: i = case insensitive m = multiline x = can contain comments l = locale s = dotall,"." matches everything,including newlines u = match unicode @param $enable_start_wildcard If set to anything other than TRUE,a starting line character "^" will be prepended to the search value,representing only searching for a value at the start of a new line. @param $enable_end_wildcard If set to anything other than TRUE,an ending line character "$" will be appended to the search value,representing only searching for a value at the end of a line. @usage = $this->mongo_db->like('foo','im',FALSE,TRUE); */ public function wheres($where){ /* -------------------------------------------------------------------------------- ORDER BY PARAMETERS -------------------------------------------------------------------------------- Sort the documents based on the parameters passed. To set values to descending order, you must pass values of either -1,'desc',or 'DESC',else they will be set to 1 (ASC). @usage = $this->mongo_db->where_between('foo',30); */ /* -------------------------------------------------------------------------------- LIMIT DOCUMENTS -------------------------------------------------------------------------------- Limit the result set to $x number of documents @usage = $this->mongo_db->limit($x); */ /* -------------------------------------------------------------------------------- OFFSET DOCUMENTS -------------------------------------------------------------------------------- Offset the result set to skip $x number of documents @usage = $this->mongo_db->offset($x); */ /* -------------------------------------------------------------------------------- GET_WHERE -------------------------------------------------------------------------------- Get the documents based upon the passed parameters @usage = $this->mongo_db->get_where('foo',array('bar' => 'something')); */ public function listinfo($collection = "",$orderby=array(),$page=1,$pagesize=12) { /* -------------------------------------------------------------------------------- GET -------------------------------------------------------------------------------- Get the documents based upon the passed parameters @usage = $this->mongo_db->get('foo',array('bar' => 'something')); */ public function getMy($collection = "") { /* -------------------------------------------------------------------------------- COUNT -------------------------------------------------------------------------------- Count the documents based upon the passed parameters @usage = $this->mongo_db->get('foo'); */ / -------------------------------------------------------------------------------- INSERT -------------------------------------------------------------------------------- Insert a new document into the passed collection @usage = $this->mongo_db->insert('foo',$data = array()); / public function insertWithId($collection = "",$data = array()) { /* -------------------------------------------------------------------------------- UPDATE_ALL -------------------------------------------------------------------------------- Insert a new document into the passed collection @usage = $this->mongo_db->update_all('foo',$data = array()); */ /* -------------------------------------------------------------------------------- DELETE -------------------------------------------------------------------------------- delete document from the passed collection based upon certain criteria @usage = $this->mongo_db->delete('foo',$data = array()); */ /* -------------------------------------------------------------------------------- DELETE_ALL -------------------------------------------------------------------------------- Delete all documents from the passed collection based upon certain criteria @usage = $this->mongo_db->delete_all('foo',$data = array()); */ /* -------------------------------------------------------------------------------- ADD_INDEX -------------------------------------------------------------------------------- Ensure an index of the keys in a collection with optional parameters. To set values to descending order,else they will be set to 1 (ASC). @usage = $this->mongo_db->add_index($collection,array('first_name' => 'ASC','last_name' => -1),array('unique' => TRUE)); / /* -------------------------------------------------------------------------------- REMOVE_INDEX -------------------------------------------------------------------------------- Remove an index of the keys in a collection. To set values to descending order,else they will be set to 1 (ASC). @usage = $this->mongo_db->remove_index($collection,'last_name' => -1)); / /* -------------------------------------------------------------------------------- REMOVE_ALL_INDEXES -------------------------------------------------------------------------------- Remove all indexes from a collection. @usage = $this->mongo_db->remove_all_index($collection); */ /* -------------------------------------------------------------------------------- LIST_INDEXES -------------------------------------------------------------------------------- Lists all indexes in a collection. @usage = $this->mongo_db->list_indexes($collection); */ /* -------------------------------------------------------------------------------- DROP COLLECTION -------------------------------------------------------------------------------- Removes the specified collection from the database. Be careful because this can have some very large issues in production! / /* -------------------------------------------------------------------------------- CLEAR -------------------------------------------------------------------------------- Resets the class variables to default settings */ /* -------------------------------------------------------------------------------- WHERE INITIALIZER -------------------------------------------------------------------------------- Prepares parameters for insertion in $wheres array(). */ public function error($str,$t) { } ?> 使用范例: table_name));
$this->mongo_db->where($where);
$order=!emptyempty($order)?array('AID'=>'DESC'):array('AID'=>'ASC');//升序降序
$infos=$this->mongo_db->listinfo($table_name,$order,$pagesize);
这篇文章就到这结束了,大家可以多学习一下 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |