php实现的mongodb操作类实例


Posted in PHP onApril 03, 2015

本文实例讲述了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://"; 
    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); 
    } 
  } 
  /**
  *
  * 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');
  */ 
  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); 
  } 
  /**
  *
  * 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');
  */ 
  public function where($wheres = array()) { 
    foreach ((array)$wheres as $wh => $val) { 
      $this->wheres[$wh] = $val; 
    } return($this); 
  } 
  /**
  *
  * 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');
  */ 
  public function where_in($field = "", $in = array()) { 
    $this->where_init($field); 
    $this->wheres[$field]['$in'] = $in; 
    return($this); 
  } 
  /**
  *
  * 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', array('bar', 'zoo', 'blah'))->get('foobar');
  */ 
  public function where_not_in($field = "", $in = array()) { 
    $this->where_init($field); 
    $this->wheres[$field]['$nin'] = $in; 
    return($this); 
  } 
  /**
  *
  * 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);
  */ 
  public function where_gt($field = "", $x) { 
    $this->where_init($field); 
    $this->wheres[$field]['$gt'] = $x; 
    return($this); 
  } 
  /**
  *
  * 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);
  */ 
  public function where_gte($field = "", $x) { 
    $this->where_init($field); 
    $this->wheres[$field]['$gte'] = $x; 
    return($this); 
  } 
  /**
  *
  * 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);
  */ 
  public function where_lt($field = "", $x) { 
    $this->where_init($field); 
    $this->wheres[$field]['$lt'] = $x; 
    return($this); 
  } 
  /**
  *
  * 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);
  */ 
  public function where_lte($field = "", $x) { 
    $this->where_init($field); 
    $this->wheres[$field]['$lte'] = $x; 
    return($this); 
  } 
  /**
  *
  * 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);
  */ 
  public function where_between($field = "", $x, $y) { 
    $this->where_init($field); 
    $this->wheres[$field]['$gte'] = $x; 
    $this->wheres[$field]['$lte'] = $y; 
    return($this); 
  } 
  /**
  *
  * 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', 20, 30);
  */ 
  public function where_between_ne($field = "", $x, $y) { 
    $this->where_init($field); 
    $this->wheres[$field]['$gt'] = $x; 
    $this->wheres[$field]['$lt'] = $y; 
    return($this); 
  } 
  /**
  *
  * 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', 20, 30);
  */ 
  public function where_ne($field = "", $x) { 
    $this->where_init($field); 
    $this->wheres[$field]['$ne'] = $x; 
    return($this); 
  } 
  /**
  *
  * 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' );
  */ 
  public function where_or($field = "", $values) { 
    $this->where_init($field); 
    $this->wheres[$field]['$or'] = $values; 
    return($this); 
  } 
  /**
  *
  * WHERE AND  *
  *
  * Get the documents where the elements match 
  the specified values *
  * @usage = $this->mongo_db->where_and( array ( 'foo' => 1, 'b' => 'someexample' );
  */ 
  public function where_and($elements_values = array()) { 
    foreach ((array)$elements_values as $element => $val) { 
      $this->wheres[$element] = $val; 
    } return($this); 
  } 
  /**
  *
  * WHERE MOD  *
  *
  * Get the documents where $field % $mod = $result *
  * @usage = $this->mongo_db->where_mod( 'foo', 10, 1 );
  */ 
  public function where_mod($field, $num, $result) { 
    $this->where_init($field); 
    $this->wheres[$field]['$mod'] = array($num, $result); 
    return($this); 
  } 
  /**   *    *  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'); */ 
  public function where_size($field = "", $size = "") { 
    $this->_where_init($field); 
    $this->wheres[$field]['$size'] = $size; 
    return ($this); 
  } 
  /**
  *
  * 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', 'bar', 'im', FALSE, TRUE);
  */ 
  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; 
  } 
  /**
  *
  * ORDER BY PARAMETERS *
  *
  * Sort the documents based on the parameters passed.
  To set values to descending order,
  * you must pass values of either -1, FALSE,
  'desc', or 'DESC', else they will be
  * set to 1 (ASC).
  *
  * @usage = $this->mongo_db->where_between('foo', 20, 30);
  */ 
  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); 
  } 
  /**
  *
  * LIMIT DOCUMENTS *
  *
  * Limit the result set to $x number of documents  *
  * @usage = $this->mongo_db->limit($x);
  */ 
  public function limit($x = 99999) { 
    if ($x !== NULL && is_numeric($x) && $x >= 1) { 
      $this->limit = (int) $x; 
    } return($this); 
  } 
  /**
  *
  * OFFSET DOCUMENTS   *
  *
  * Offset the result set to skip $x number of documents
  *
  * @usage = $this->mongo_db->offset($x);
  */ 
  public function offset($x = 0) { 
    if ($x !== NULL && is_numeric($x) && $x >= 1) { 
      $this->offset = (int) $x; 
    } return($this); 
  } 
  /**
  *
  * GET_WHERE  *
  *  
  * Get the documents based upon the passed parameters  *
  * @usage = $this->mongo_db->get_where('foo', array('bar' => 'something'));
  */ 
  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; 
  } 
  /**
  *
  * GET *
  *
  * Get the documents based upon the passed parameters  *
  * @usage = $this->mongo_db->get('foo', array('bar' => 'something'));
  */ 
  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); 
  } 
  /**
  *
  * COUNT  *
  *
  * Count the documents based upon the passed parameters *
  * @usage = $this->mongo_db->get('foo');
  */ 
  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); 
  } 
  /**
  *
  * INSERT  *
  *
  * Insert a new document into the passed collection *
  * @usage = $this->mongo_db->insert('foo', $data = array());
  */ 
  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 { 
    /**
      wxcity_base::load_sys_class('whtysqs','',0); 
      $mongoseq_class = new whtysqs('creaseidsqs');
      $re = $mongoseq_class->query("?name=" . $collection . "&opt=put&data=1");
    **/ 
      $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); 
    } 
  } 
  /**
  *
  * UPDATE  *
  *
  * Update a document into the passed collection *
  * @usage = $this->mongo_db->update('foo', $data = array());
  */ 
  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); 
    } 
  } 
  /**
  *
  * UPDATE_ALL  *
  *
  * Insert a new document into the passed collection *
  * @usage = $this->mongo_db->update_all('foo', $data = array());
  */ 
  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); 
    } 
  } 
  /**
  *
  * DELETE  *
  *
  * delete document from the passed collection based upon certain criteria *
  * @usage = $this->mongo_db->delete('foo', $data = array());
  */ 
  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); 
    } 
  } 
  /**
  *
  * DELETE_ALL  *
  *
  * Delete all documents from the passed collection based upon certain criteria
  *
  * @usage = $this->mongo_db->delete_all('foo', $data = array());
  */ 
  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); 
    } 
  } 
  /**
  *
  * ADD_INDEX  *
  *
  * Ensure an index of the keys in a collection with optional parameters.
  To set values to descending order,
  * you must pass values of either -1, FALSE, 'desc', or 'DESC', 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));
  */ 
  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); 
    } 
  } 
  /**
  *
  * REMOVE_INDEX   *
  *
  * Remove an index of the keys in a collection.
  To set values to descending order,
  * you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be
  * set to 1 (ASC). *
  * @usage = $this->mongo_db->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1));
  */ 
  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); 
    } 
  } 
  /**
  *
  * REMOVE_ALL_INDEXES  *
  *
  * Remove all indexes from a collection. *
  * @usage = $this->mongo_db->remove_all_index($collection);
  */ 
  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); 
  } 
  /**
  *
  * LIST_INDEXES   *
  *
  * Lists all indexes in a collection. *
  * @usage = $this->mongo_db->list_indexes($collection);
  */ 
  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()); 
  } 
  /**
  *
  * DROP COLLECTION *
  *
  * Removes the specified collection from the database.
  Be careful because this
  * can have some very large issues in production!
  */ 
  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; 
  } 
  /**
  *
  * CLEAR  *
  *
  * Resets the class variables to default settings
  */ 
  private function clear() { 
    $this->selects = array(); 
    $this->wheres = array(); 
    $this->limit = NULL; 
    $this->offset = NULL; 
    $this->sorts = array(); 
  } 
  /**
  *
  * WHERE INITIALIZER  *
  *
  * Prepares parameters for insertion in $wheres 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);

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP面向对象编程快速入门
Oct 09 PHP
PHP导出EXCEL快速开发指南--PHPEXCEL的使用详解
Jun 03 PHP
php递归获取目录内文件(包含子目录)封装类分享
Dec 25 PHP
PHP内核探索:变量概述
Jan 30 PHP
php析构函数的简单使用说明
Aug 24 PHP
laravel学习教程之关联模型
Jul 30 PHP
PHP用户管理中常用接口调用实例及解析(含源码)
Mar 09 PHP
CI(CodeIgniter)框架实现图片上传的方法
Mar 24 PHP
三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
Nov 13 PHP
PHP以json或xml格式返回请求数据的方法
May 31 PHP
php使用fullcalendar日历插件详解
Mar 06 PHP
php和C#的yield迭代器实现方法对比分析
Jul 17 PHP
PHP中判断文件存在使用is_file还是file_exists?
Apr 03 #PHP
php专用数组排序类ArraySortUtil用法实例
Apr 03 #PHP
Win7下手动安装apache2.2、php5.4笔记
Apr 03 #PHP
PHP中返回引用类型的方法
Apr 03 #PHP
PHP中应该避免使用同名变量(拆分临时变量)
Apr 03 #PHP
PHP5多态性与动态绑定介绍
Apr 03 #PHP
php5.4以上版本GBK编码下htmlspecialchars输出为空问题解决方法汇总
Apr 03 #PHP
You might like
PHP游戏编程25个脚本代码
2011/02/08 PHP
PHP无限分类代码,支持数组格式化、直接输出菜单两种方式
2011/05/18 PHP
php中长文章分页显示实现代码
2012/09/29 PHP
PHP面向对象编程之深入理解方法重载与方法覆盖(多态)
2015/12/24 PHP
CI框架整合widget(页面格局)的方法
2016/05/17 PHP
解决PHP程序运行时:Fatal error: Maximum execution time of 30 seconds exceeded in的错误提示
2016/11/25 PHP
PHP与SQL语句常用大全
2016/12/10 PHP
Laravel学习基础之migrate的使用教程
2017/10/11 PHP
php析构函数的具体用法小结
2014/03/11 Javascript
node.js中的console.error方法使用说明
2014/12/10 Javascript
jQuery增加自定义函数的方法
2015/07/18 Javascript
jquery插件tytabs.jquery.min.js实现渐变TAB选项卡效果
2015/08/25 Javascript
jQuery热气球动画半透明背景的后台登录界面代码分享
2015/08/28 Javascript
js面向对象编程总结
2017/02/16 Javascript
vue组件Prop传递数据的实现示例
2017/08/17 Javascript
webpack使用 babel-loader 转换 ES6代码示例
2017/08/21 Javascript
node.js 利用流实现读写同步,边读边写的方法
2017/09/11 Javascript
Postman的下载及安装教程详解
2018/10/16 Javascript
移动端手指操控左右滑动的菜单
2019/09/08 Javascript
javascript History对象原理解析
2020/02/17 Javascript
何时/使用 Vue3 render 函数的教程详解
2020/07/25 Javascript
基于Vue全局组件与局部组件的区别说明
2020/08/11 Javascript
python Django模板的使用方法
2016/01/14 Python
python 爬取微信文章
2016/01/30 Python
动态规划之矩阵连乘问题Python实现方法
2017/11/27 Python
详解用python实现简单的遗传算法
2018/01/02 Python
HTML5 source标签:媒介元素定义媒介资源
2018/01/29 HTML / CSS
Nisbets爱尔兰:英国最大的厨房和餐饮设备供应商
2019/01/26 全球购物
澳大利亚女装精品店:Alannah Hill
2020/07/29 全球购物
serialVersionUID具有什么样的特征
2014/02/20 面试题
省级优秀班集体申报材料
2014/05/25 职场文书
2015年办公室工作总结范文
2015/03/31 职场文书
党员学习中国梦心得体会
2016/01/05 职场文书
ORACLE数据库对long类型字段进行模糊匹配的解决思路
2021/04/07 Oracle
mysql如何配置白名单访问
2021/06/30 MySQL
vue生命周期钩子函数以及触发时机
2022/04/26 Vue.js