您现在的位置是:网站首页> 编程资料编程资料
高效mongodb的php分页类(不使用skip)_MongoDB_
2023-05-27
383人已围观
简介 高效mongodb的php分页类(不使用skip)_MongoDB_
mongodb分页skip+limit分页要先查出所有结果再去跳过,这样如果查询页面越往后效率越低。
如果能够通过查询条件查出每页结果的最后一条记录,在用最后一条记录作为查询条件去查下一页,这样每次都查询页面size条记录,效率不会差。
具体代码如下:包含mongodb.class.php, page.class.php, test.php
mongodb.class.php mongodb 操作类
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
点击排行
本栏推荐
