加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php封装的mongodb操作类代码

发布时间:2021-02-25 16:12:55 所属栏目: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

/* -------------------------------------------------------------------------------- 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 (!emptyempty($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 ((array)$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','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);
}

(编辑:济源站长网)

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

热点阅读