加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (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

/* -------------------------------------------------------------------------------- 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',30); */
public function where_between_ne($field = "",$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',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 values @usage = $this->mongo_db->where_or('foo',array( 'foo','bar','blegh' ); */
public function where_or($field = "",$values) {
$this->where_init($field);
$this->wheres[$field]['$or'] = $values;
return($this);
}

/* -------------------------------------------------------------------------------- WHERE AND -------------------------------------------------------------------------------- Get the documents where the elements match the specified values @usage = $this->mongo_db->where_and( array ( 'foo' => 1,'b' => 'someexample' ); */
public function where_and($elements_values = array()) {
foreach ((array)$elements_values as $element => $val) {
$this->wheres[$element] = $val;
} return($this);
}

/* -------------------------------------------------------------------------------- WHERE MOD -------------------------------------------------------------------------------- Get the documents where $field % $mod = $result @usage = $this->mongo_db->where_mod( 'foo',10,1 ); */
public function where_mod($field,$num,$result) {
$this->where_init($field);
$this->wheres[$field]['$mod'] = array($num,$result);
return($this);
}

/* -------------------------------------------------------------------------------- Where size -------------------------------------------------------------------------------- Get the documents where the size of a field is in a given $size int @usage : $this->mongo_db->where_size('foo',1)->get('foobar'); */
public function where_size($field = "",$size = "") {
$this->_where_init($field);
$this->wheres[$field]['$size'] = $size;
return ($this);
}

/* -------------------------------------------------------------------------------- LIKE PARAMETERS -------------------------------------------------------------------------------- Get the documents where the (string) value of a $field is like a value. The defaults allow for a case-insensitive search. @param $flags Allows for the typical regular expression flags: i = case insensitive m = multiline x = can contain comments l = locale s = dotall,"." matches everything,including newlines u = match unicode @param $enable_start_wildcard If set to anything other than TRUE,a starting line character "^" will be prepended to the search value,representing only searching for a value at the start of a new line. @param $enable_end_wildcard If set to anything other than TRUE,an ending line character "$" will be appended to the search value,representing only searching for a value at the end of a line. @usage = $this->mongo_db->like('foo','im',FALSE,TRUE); */
public function like($field = "",$value = "",$flags = "i",$enable_start_wildcard = TRUE,$enable_end_wildcard = TRUE) {
$field = (string) trim($field);
$this->where_init($field);
$value = (string) trim($value);
$value = quotemeta($value);
if ($enable_start_wildcard !== TRUE) {
$value = "^" . $value;
} if ($enable_end_wildcard !== TRUE) {
$value .= "$";
} $regex = "/$value/$flags";
$this->wheres[$field] = new MongoRegex($regex);
return($this);
}

public function wheres($where){
$this->wheres = $where;
}

(编辑:济源站长网)

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

热点阅读