mongodb分页skip+limit分页要先查出所有结果再去跳过,这样如果查询页面越往后效率越低。
如果能够通过查询条件查出每页结果的最后一条记录,在用最后一条记录作为查询条件去查下一页,这样每次都查询页面size条记录,效率不会差。
具体代码如下:包含mongodb.class.php, page.class.php, test.php
mongodb.class.php mongodb 操作类
//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)
{