加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php封装的mongodb操作类代码

发布时间:2021-02-28 18:23:08 所属栏目:PHP教程 来源:网络整理
导读:核心代码 /* To change this template,choose Tools | Templates and open the template in the editor. */ class mongo_db { private $config; private $connection; private $db; private $connection_string; private $host; private $port; private $use

/* -------------------------------------------------------------------------------- ORDER BY PARAMETERS -------------------------------------------------------------------------------- Sort the documents based on the parameters passed. To set values to descending order, you must pass values of either -1,'desc',or 'DESC',else they will be set to 1 (ASC). @usage = $this->mongo_db->where_between('foo',30); */
public function order_by($fields = array()) {
if (!is_array($fields) || !count($fields)) return ;
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,$orderby=array()) {
if (is_array($orderby) || !emptyempty($orderby)) {
$order_by = $this->order_by($order_by);
}
return($this->where($where)->limit($limit)->get($collection));
}
public function selectA($collection = "",$orderby=array()) {
if(intval($limit)<1){
$limit = 999999;
}
$order_by = $this->order_by($orderby);
$re = $this->limit($limit)->get($collection);
$this->clear();
return (array)$re;
}

public function listinfo($collection = "",$orderby=array(),$page=1,$pagesize=12) {
$page = max(intval($page),1);
$offset = $pagesize * ($page - 1);
$pagesizes = $offset + $pagesize;
$this->offset($offset);
$order_by = $this->order_by($orderby);
$re = $this->limit($pagesize)->get($collection);
$this->limit(999999);
$count = $this->count($collection);
$this->pages = pages($count,$page,$pagesize);
return (array)$re;
}

/* -------------------------------------------------------------------------------- GET -------------------------------------------------------------------------------- Get the documents based upon the passed parameters @usage = $this->mongo_db->get('foo',array('bar' => 'something')); */
public function get($collection = "") {
if (emptyempty($collection)) {
$this->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;
return($returns);
}

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读