欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

高效mongodb的php分页类(不使用skip)

程序员文章站 2022-09-03 23:17:29
mongodb分页skip+limit分页要先查出所有结果再去跳过,这样如果查询页面越往后效率越低。 如果能够通过查询条件查出每页结果的最后一条记录,在用最后一条记录作为...

mongodb分页skip+limit分页要先查出所有结果再去跳过,这样如果查询页面越往后效率越低。

如果能够通过查询条件查出每页结果的最后一条记录,在用最后一条记录作为查询条件去查下一页,这样每次都查询页面size条记录,效率不会差。

具体代码如下:包含mongodb.class.php, page.class.php, test.php

mongodb.class.php mongodb 操作类

复制代码 代码如下:

<?php
function show_error($message, $status_code = 500, $heading = 'an error was encountered')
{
        echo $message, $status_code,php_eol;
        exit;
}

//mongodb操作类
class db
{

 private $ci;
 private $config_file = 'mongodb';

 private $connection;
 private $db;
 private $connection_string;

 private $collection = '';
 private $host;
 private $port;
 private $user;
 private $pass;
 private $dbname;
 private $key;
 private $persist;
 private $persist_key;

 private $selects = array();
 private $wheres = array();
 private $sorts = array();
 private $page_sorts = array();

 private $limit = 999999;
 private $offset = 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($mongodb_config)
 {
  if(!class_exists('mongo'))
  {
   show_error("the mongodb pecl extension has not been installed or enabled", 500);
  }
  /**
        $config['mongo_host'] = '221.234.43.144';
        $config['mongo_port'] = 27017;
        $config['mongo_db'] = 'test';
        $config['mongo_user'] = '';
        $config['mongo_pass'] = '';
        $config['mongo_persist'] = true;
         *
         */
  $this->connection_string($mongodb_config);
  $this->connect();
 }

 

 /**
  * --------------------------------------------------------------------------------
  * switch_db
  * --------------------------------------------------------------------------------
  *
  * switch from default database to a different db
  */

 public function switch_db($database = '')
 {
  if(empty($database))
  {
   show_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)
  {
   show_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(!empty($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($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 ( $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);
  }

 /**
  * --------------------------------------------------------------------------------
  * 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())
  {
   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)
  {
   return($this->where($where)->limit($limit)->get($collection));
  }

 /**
  * --------------------------------------------------------------------------------
  * get
  * --------------------------------------------------------------------------------
  *
  * get the documents based upon the passed parameters
  *
  * @usage = $this->mongo_db->get('foo', array('bar' => 'something'));
  */

  public function get($collection = "")
  {
   if(empty($collection))
   {
    show_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(empty($collection))
   {
    show_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);
  }

 /**
  * 自增id实现
  * return insert_id
  */
 private function insert_inc($table)
 {
  $update = array('$inc'=>array('id'=>1));
  $query = array('table'=>$table);
  $command = array(
  'findandmodify'=>'_increase',
  'update'=>$update,
  'query'=>$query,
  'new'=>true,
  'upsert'=>true
  );
  $id = $this->db->command($command);
  return $id['value']['id'];
 }

 /**
  * --------------------------------------------------------------------------------
  * insert
  * --------------------------------------------------------------------------------
  *
  * insert a new document into the passed collection
  *
  * @usage = $this->mongo_db->insert('foo', $data = array());
  */

  public function insert($collection = "", $data = array()) {
   if(empty($collection))
   {
    show_error("no mongo collection selected to insert into", 500);
   }

   if(count($data) == 0 || !is_array($data))
   {
    show_error("nothing to insert into mongo collection or insert is not an array", 500);
   }

   try
   {
   $inc = $this->insert_inc($collection);
   $data['_id'] = $inc;
    $result = $this->db->{$collection}->insert($data, array('fsync' => true));
    if($result['ok'] || $result){
    return true;
   }
   else{
    return false;
   }
   }
   catch(mongocursorexception $e)
   {
    show_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(), $flage = false)
  {
   if(empty($collection))
   {
    show_error("no mongo collection selected to update", 500);
  }
   if(count($data) == 0 || !is_array($data))
   {
    show_error("nothing to update in mongo collection or update is not an array", 500);
  }
  unset($data['_id']);
  if($flage){
   $arr = $this->wheres;
   unset($arr['_id']);
   if(is_array($arr)){
    foreach($arr as $key => $w){
     unset($data[$key]);
    }
   }
  }
  try
   {
    $res = $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => true, 'multiple' => false));
   $this->clear();
   return $res;
   } 
   catch(mongocursorexception $e)
   {
    show_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(empty($collection))
   {
    show_error("no mongo collection selected to update", 500);
   }

   if(count($data) == 0 || !is_array($data))
   {
    show_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));
   $this->clear();
    return(true);
   }
   catch(mongocursorexception $e)
   {
    show_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, $where)
  {
   if(empty($collection))
   {
    show_error("no mongo collection selected to delete from", 500);
   }
  if(!$where){
   show_error("no data input to delete", 500);
  }
   try
   {
   $this->wheres = $where;
    $this->db->{$collection}->remove($this->wheres);
   $this->clear();
    return(true);
   }
   catch(mongocursorexception $e)
   {
    show_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(empty($collection))
     {
      show_error("no mongo collection selected to delete from", 500);
    }

   try
   {
    $this->db->{$collection}->remove($this->wheres, array('fsync' => true, 'justone' => false));
   $this->clear();
    return(true);
   }
   catch(mongocursorexception $e)
   {
    show_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(empty($collection))
  {
    show_error("no mongo collection specified to add index to", 500);
   }

   if(empty($keys) || !is_array($keys))
   {
    show_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
   {
    show_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(empty($collection))
  {
    show_error("no mongo collection specified to remove index from", 500);
   }

   if(empty($keys) || !is_array($keys))
   {
    show_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
   {
    show_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(empty($collection))
  {
    show_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(empty($collection))
  {
    show_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(empty($collection))
   {
      show_error("no mongo collection specified to drop from database", 500);
    }
    $this->db->{$collection}->drop();
    return true;
  }


 /**
  * --------------------------------------------------------------------------------
  * 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() {
  $options = array();
  if($this->persist === true)
  {
   $options['persist'] = isset($this->persist_key) && !empty($this->persist_key) ? $this->persist_key : 'ci_mongo_persist';
  }

  try
  {
   $this->connection = new mongo($this->connection_string, $options);
   $this->db = $this->connection->{$this->dbname};
   return($this);
  }
  catch(mongoconnectionexception $e)
  {
   show_error("unable to connect to mongodb: {$e->getmessage()}", 500);
  }
 }

 /**
  * --------------------------------------------------------------------------------
  * build connection string
  * --------------------------------------------------------------------------------
  *
  * build the connection string from the config file.
  */

 private function connection_string($mongodb_config)
 {

  $this->host = trim($mongodb_config['host']);
  $this->port = trim($mongodb_config['port']);
  $this->user = trim($mongodb_config['user']);
  $this->pass = trim($mongodb_config['pwd']);
  $this->dbname = trim($mongodb_config['database']);
  $this->persist = trim($mongodb_config['persist']);
  $this->persist_key = trim($mongodb_config['persist_key']);

  $connection_string = "mongodb://";

  if(empty($this->host))
  {
   show_error("the host must be set to connect to mongodb", 500);
  }

  if(empty($this->dbname))
  {
   show_error("the database must be set to connect to mongodb", 500);
  }

  if(!empty($this->user) && !empty($this->pass))
  {
   $connection_string .= "{$this->user}:{$this->pass}@";
  }

  if(isset($this->port) && !empty($this->port))
  {
   $connection_string .= "{$this->host}:{$this->port}/{$this->dbname}";
  }
  else
  {
   $connection_string .= "{$this->host}";
  }

  $this->connection_string = trim($connection_string);
 }

 /**
  * --------------------------------------------------------------------------------
  * 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();
   }
 }

 /**
  * --------------------------------------------------------------------------------
  * 设置表
  * --------------------------------------------------------------------------------
  *  参数:
  *  $table 表名
  */
 public function set_table($table){
  $this->collection = $table;
 }

 /**
  * --------------------------------------------------------------------------------
  * 获取表名
  * --------------------------------------------------------------------------------
  */
 public function get_table(){
  return $this->collection;
 }

 /**
  * --------------------------------------------------------------------------------
  * 设置表排序
  * --------------------------------------------------------------------------------
  *  参数:
  *  $orderby 排序
  */
 public function set_orderby($orderby){
  $this->page_sorts = $orderby;
 }

 
 /**
  * --------------------------------------------------------------------------------
  * 获取左边结果集
  * --------------------------------------------------------------------------------
  *  参数:
  *  $left 左边显示的个数
  *  $last 定位当前页的值
  *  $size 页面大小
  */
 public function get_left($left, $last, $size = page_size){
  if($last){
   $order = $this->nor_orderby();
   if($this->page_sorts[$this->key] == -1){
    $this->where_gt($this->key, $last);
   } else {
     $this->where_lt($this->key, $last);
   }
   return $this->limit($left * $size)->order_by($order)->get($this->collection);
  }
 }

 
 /**
  * --------------------------------------------------------------------------------
  * 获取右边结果集
  * --------------------------------------------------------------------------------
  *  参数:
  *  $right 右边显示的个数
  *  $last 定位当前页的值
  *  $size 页面大小
  */
 public function get_right($right, $last, $size = page_size){
  if($last){
   if($this->page_sorts[$this->key] == -1){
    $this->where_lte($this->key, $last);
   } else {
    $this->where_gte($this->key, $last);
   }
  }
  return $this->limit($right * $size + 1)->order_by($this->page_sorts)->get($this->collection);
 }

 /**
  * --------------------------------------------------------------------------------
  * 设置key
  * --------------------------------------------------------------------------------
  *  参数:
  *  $key 设置索引主键
  */
 public function set_key($key){
  $this->key = $key;
 }

 /**
  * --------------------------------------------------------------------------------
  * 求反
  * --------------------------------------------------------------------------------
  */
 private function nor_orderby(){
  foreach($this->page_sorts as $key => $order){
   if($order == -1){
    $orderby[$key] = 1;
   }else{
    $orderby[$key] = -1;
   }  
  }
  return $orderby;
 }

 /**
  * --------------------------------------------------------------------------------
  * 获取上一页的值
  * --------------------------------------------------------------------------------
  *  参数:
  *  $last 定位当前页的值
  *  $size 页面大小
  */
 public function get_prev($last, $size = page_size){
  if($last){
   if($this->page_sorts[$this->key] == 1){
    $this->where_lt($this->key,$last)->order_by(array($this->key => -1));
   } else {
    $this->where_gt($this->key,$last)->order_by(array($this->key => 1));
   }
   $result = $this->limit($size)->get($this->collection);
  }
  return $result[$size - 1][$this->key];
 }

  /**
  * --------------------------------------------------------------------------------
  * 获取下一页的值
  * --------------------------------------------------------------------------------
  *  参数:
  *  $last 定位当前页的值
  *  $size 页面大小
  */
 public function get_next($last, $size = page_size){
  if($last){
   if($this->page_sorts[$this->key] == 1){
    $this->where_gte($this->key,$last);
   } else {
    $this->where_lte($this->key,$last);
   }
  }
  $result = $this->limit($size+1)->order_by($this->page_sorts)->get($this->collection);
  return $result[$size][$this->key];
 }

 /**
  * --------------------------------------------------------------------------------
  * 获取最后一页的值
  * --------------------------------------------------------------------------------
  *  参数:
  *  $size 页面大小
  */
 public function get_last($size = page_size){
  $res = $this->count($this->collection) % $size;
  $order = $this->nor_orderby();
  if($res > 0){
   $result = $this->limit($res)->order_by($order)->get($this->collection);
   return $result[$res - 1][$this->key];
  }else{
   $result = $this->limit($size)->order_by($order)->get($this->collection);
   return $result[$size - 1][$this->key];
  }
 }

 /**
  * --------------------------------------------------------------------------------
  * 分页查询
  * --------------------------------------------------------------------------------
  *  参数:
  *  $last 定位当前页的值
  *  $size 页面大小
  */
 public function page_query($last, $size = page_size){
  if($last){
   if($this->page_sorts[$this->key]==1){
    $this->where_gte($this->key,$last);
   } else {
    $this->where_lte($this->key,$last);
   }
  }
  return $this->limit($size)->order_by($this->page_sorts)->get($this->collection);
 }

 /**
  * 批量执行代码_插入
  * @param string $collection
  * @param 二维数组 $code
  */
 public function execute_insert($collection,$code){
  //将二维数组分成js格式
  $strcode='';
  foreach($code as $k=>$v){
   foreach($v as $kk=>$vv){
    $strcode.='db.getcollection("'.$collection.'").insert({ "'.$kk.'":"'.$vv.'" });';
   }   
  }
 // retrun array([ok]=>1);
  return $this->db->execute($code); 
 }

}
?>


page.class.php mongodb分页逻辑类
复制代码 代码如下:

<?php
db = $db;
  $this->count = $this->db->count($this->db->get_table());
  $url = site_root.strtolower(class_name).'/'.method_name;
  $this->url = $this->url ? $this->url : $url;
  $set = $set ? $set : 5;
  $this->set = $set;
  $size = $size ? $size : page_size;
  $this->size = $size;
  $this->last = $last;
  $this->prev = $db->get_prev($this->last);
  $this->next = $db->get_next($this->last);
  //$this->page = get::uint('page');
  $this->page = $this->page ? $this->page : 1;
  $this->total = @ceil($this->count / $this->size);
  $this->key = $key;
  $this->orderby = $orderby;
 }

 //输出分页链接
 public function get_link(){
  if($this->total != 1){
   $this->get_first();
   $this->get_prev();
   $this->get_center();
   $this->get_next();
   $this->get_last();
   $this->get_turnto();
  }
  if($this->link){
   $this->link = $this->turnto.$this->link.'共'.number_format($this->total).'页 '.number_format($this->count).'条记录';
  }
  if($this->turnto){
   $this->link .= '';
  }
  return $this->link;
 }

 //获取左边显示的个数
 public function get_left(){
  return  $this->left = ($this->set - $this->page >= 0) ? ($this->page - 1) : $this->set;
 }

 //获取右边显示的个数
 public function get_right(){
  return $this->right = ($this->total - $this->page > $this->set) ? $this->set : ($this->total - $this->page);
 }

 //设置左边的结果集
 public function set_left_result($left_result){
  $this->leftresult = $left_result;
 }

 //设置右边的结果集
 public function set_right_result($right_result){
  $this->rightresult = $right_result;
 }

 //设置排序条件
 public function set_orderby($orderby){
  $this->orderby = $orderby;
 }

 //设置最后一页
 public function set_last($last){
  $this->lastd = $last;
 }

 //设置中间显示页码个数
 public function set($set){
  $this->set = $set;
 }

 //获取首页
 private function get_first(){
  if($this->page != 1){
   if($this->total > 0){
    $this->link.='首页';
   }
  }
 }

 //获取上一页
 private function get_prev(){
  if($this->prev){
   $this->link.='上一页';
  }
 }

 //中间显示
 private function get_center(){
  $start = ($this->page - $this->set) <= 0 ? 1 : ($this->page - $this->set); 
  $end = ($this->page + $this->set + 1 >= $this->total) ? $this->total + 1 : ($this->page + $this->set + 1);

  $ii = $this->left;
  $iii = 0;
  //显示左边的
  for($i = $start; $i < $end; $i++, $ii--, $iii++){
   if($this->page == $i){
    $this->link.=''.$i.'';
   }else{
    $the_id = $ii * $this->size - 1;
    if($the_id > 0){
     $this->link.=''.$i.'';
    }else{
     $the_id = ($iii - $this->left) * $this->size;
     $this->link.=''.$i.'';
    }   
   }
  }
 }

 //获取下一页
 private function get_next(){
  if($this->next){
   $this->link.='下一页';
  }
 }

 //获取尾页
 private function get_last(){
  if($this->page != $this->total){
   $this->link.='尾页';
  }
 }

 //跳转到
 private function get_turnto(){
  $this->turnto = '
转到第 <input type="text" name="p" style="width:25px;text-align:center"> 页';
 }

 //求反
 public function nor_orderby(){
  foreach($this->orderby as $key => $order){
   if($order==-1){
    $orderby[$key] = 1;
   }else{
    $orderby[$key] = -1;
   }  
  }
  return $orderby;
 }

 //设置key
 public function set_key($key){
  $this->key = $key;
 }

 //分页操作
 public function show(){
  $this->set_key($this->key);
  $this->set_orderby($this->orderby);
  $left = $this->get_left();
  $right = $this->get_right();
  $leftresult = $this->db->get_left($left, $this->last);
  $rightresult = $this->db->get_right($right, $this->last);
  $this->set_left_result($leftresult);
  $this->set_right_result($rightresult);
  $last = $this->db->get_last();
  $this->set_last($last);
  return $this->get_link();
 }
}
/*      调用例子rockmongo
  global $db;
  $lastid = get::uint('id');
  $table = 'log';
  $key = '_id';
  $orderby = array($key => -1);

  $db->set_table($table);
  $db->set_key($key);
  $db->set_orderby($orderby);

  $log = $db->page_query($lastid);

  $page = new page($lastid, $key, $orderby);
  $pager = $page->show();
*/

?>


test.php 测试代码
复制代码 代码如下:

<?php
  include "page.class.php";
  include "mongodb.class.php";
  define(page_size, 5);//每页大小
  $config['host'] = '127.0.0.1';
  $config['port'] = 20081;  //mongodb端口
  $config['database'] = 'domain';//mongodb数据库名
  $config['user'] = '';
  $config['pwd'] = '';
  $config['persist'] = true;

  $db = new db($config);

  $table = 'whois'; //mongodb collection名
  $key = '_id';
  $orderby = array($key => -1);

  $db->set_table($table);
  $db->set_key($key);
  $db->set_orderby($orderby);

  $log = $db->page_query($lastid,5);

  $page = new page($lastid, $key, $orderby);

  echo $pager = $page->show();


?>