• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >MongoDB > 高效mongodb的php分页类(不使用skip)

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了mongodb skip,php mongodb,php mongodb扩展,php连接mongodb,php操作mongodb等相关知识,希望本文的分享对您有所帮助

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)
  {
 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MongoDB 使用Skip和limit分页
  • 高效mongodb的php分页类(不使用skip)

相关文章

  • 2017-05-11MongoDB使用小结:一些不常见的经验分享
  • 2017-05-11mongodb 数据类型(null/字符串/数字/日期/内嵌文档/数组等)
  • 2017-05-11mongodb eval 执行服务器端脚本
  • 2017-05-11MongoDB的PHP驱动方法与技巧
  • 2017-05-11MongoDB插入数据的3种方法
  • 2017-05-11MongoDB入门教程之聚合和游标操作介绍
  • 2017-05-11初识NoSQL NoSql数据库入门 NoSql数据库基础知识
  • 2017-05-11Mongodb如何开启用户访问控制详解
  • 2017-05-11mongodb设置后台运行的方法
  • 2017-05-11MongoDB Windows安装服务方法与注意事项

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • mongodb 数据库操作详解--创建,切换,删除
    • Mongodb常见错误与解决方法小结(Mongodb中经常出现的错误)
    • Mongodb自增id实现方法
    • MongoDB数据库forEach循环遍历用法
    • 高效mongodb的php分页类(不使用skip)
    • mongoDB 实现主从读写分离实现的实例代码
    • Mongodb中MapReduce实现数据聚合方法详解
    • mongodb 修改用户密码 2种方法
    • MongoDB快速入门笔记(四)之MongoDB查询文档操作实例代码
    • MongoDB使用自带的命令行工具进行备份和恢复的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有