欢迎来到天天文库
浏览记录
ID:9281867
大小:40.50 KB
页数:6页
时间:2018-04-26
《mongodb高效分页类》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、使用Skip和limit可以如下做数据分页:Code:page1=db.things.find().limit(20)page2=db.things.find().skip(20).limit(20)page3=db.things.find().skip(40).limit(20)当数据量很小时,这样做分页完全没有问题。但是当数据量很大时,skip操作会变的很慢,应该避免使用。(不止是mongoDb会这样,大部分数据库都是。)可以通过改变查询文档的规则来达到分页效果,避免使用skip来跳过大量的数据。(通过计算,得到下次查询应该从什么地方开始)
2、assPage{var$count='';var$size='';var$total='';var$last='';var$link='';var$url='';var$db='';var$orderby='';var$table='';var$set='';var$page='';var$turnto='';var$key='';publicfunction__construct($table='',$orderby=array(),$count='',$size='',$last='',$theurl='',$set='',$next='',$prev=''){global$D
3、B;$url=SITE_ROOT.strtolower(CLASS_NAME).'/'.METHOD_NAME;$set=$set?$set:5;$this->table=$table;$this->count=$count;$this->size=$size;$this->last=$last;$this->url=$this->url?$this->url:$url;$this->set=$set;$this->next=$next;$this->prev=$prev;$this->db=$DB;$this->orderby=$orderby;$this->page=GET::
4、UINT('page');$this->page=$this->page?$this->page:1;//$this->page=$this->page>0?$this->page:GET::UINT('search')>0?GET::UINT('search'):1*/;//$this->page=GET::UINT('search');;$this->total=@ceil($this->count/$this->size);}publicfunctionshow(){if($this->total!=1){$this->get_first();$this->get_prev(
5、);$this->get_center();$this->get_next();$this->get_last();$this->get_turnto();}if($this->link){$this->link=$this->turnto.$this->link.'共'.$this->total.'页'.$this->count.'条记录';}if($this->turnto){$this->link.='';}return$this->link;}//获取首页privatefunctionget_first(){if($this->page!=1){if($thi
6、s->total>0){$this->link.='url.'"title="首页">首页';}}}//获取上一页privatefunctionget_prev(){if($this->prev){$this->link.='url.'/page/'.($this->page-1).'/id/'.$this->prev.'"title="上一页">上一页';}}//中间显示privatefunctionget_center(){$left=($this->set-$this->page>=0)?($
7、this->page-1):$this->set;$right=($this->total-$this->page>$this->set)?$this->set:($this->total-$this->page);if(!$this->last){$rightresult=$this->db->limit($right*$this->size+1)->order_by($this->orderby)->get($this->table);}else{$rightre
此文档下载收益归作者所有