|
本文实例讲述了php操作MongoDB类的方法。分享给大家供大家参考。具体如下:
1. MyMongo.php文件:
log_error("The MongoDB PECL extentiosn has not been installed or enabled.");
exit;
}
$this->connection_string();
$this->connect();
}
/**
- 更改数据库
-
*/
public function switch_db($database = '') {
if (empty($database)) {
$this->log_error("To switch MongoDB databases,a new database name must be specified");
exit;
}
$this->dbname = $database;
try {
$this->db = $this->connection->{$this->dbname};
return true;
} catch(Exception $e) {
$this->log_error("Unable to switch Mongo Databases: {$e->getMessage()}");
exit;
}
}
/**
- 设置select字段
-
*/
public function select($includs = array(),$excludes = array()) {
if ( ! is_array($includs)) {
$includs = (array)$includs;
}
if ( ! is_array($excludes)) {
$excludes = (array)$excludes;
}
if ( ! empty($includs)) {
foreach ($includs as $col) {
$this->selects[$col] = 1;
}
} else {
foreach ($excludes as $col) {
$this->selects[$col] = 0;
}
}
return($this);
}
/**
if ( ! empty($wheres)) {
foreach($wheres as $wh => $val) {
$this->wheres[$wh] = $val;
}
}
return($this);
}
/**
- where ... in .. 条件查询判断
-
- @usage = $this->mongo_db->where_in('foo',array('bar','zoo'))->get('foobar');
-
*/
public function where_in($field = '',$in = array()) {
$this->where_init($field);
$this->wheres[$field]['$in'] = $in;
return($this);
}
/**
- where ... not in .. 条件查询判断
-
- @usage = $this->mongo_db->where_not_in('foo','zoo'))->get('foobar');
-
*/
public function where_not_in($field = '',$in = array()) {
$this->where_init($field);
$this->wheres[$field]['$nin'] = $in;
return($this);
}
/**
- where ... $field > $x .. 条件查询判断
-
- @usage = $this->mongo_db->where_gt('foo',20)->get('foobar');
-
*/
public function where_gt($field = '',$x) {
$this->where_init($field);
$this->wheres[$field]['$gt'] = $x;
return($this);
}
/**
- where ... $field >= $x .. 条件查询判断
-
- @usage = $this->mongo_db->where_gte('foo',20)->get('foobar');
-
*/
public function where_gte($field = '',$x) {
$this->where_init($field);
$this->wheres[$field]['$gte'] = $x;
return($this);
}
/**
- where ... $field < $x .. 条件查询判断
-
- @usage = $this->mongo_db->where_lt('foo',20)->get('foobar');
-
*/
public function where_lt($field = '',$x) {
$this->where_init($field);
$this->wheres[$field]['$lt'] = $x;
return($this);
}
/**
- where ... $field <= $x .. 条件查询判断
-
- @usage = $this->mongo_db->where_lte('foo',20)->get('foobar');
-
*/
public function where_lte($field = '',$x) {
$this->where_init($field);
$this->wheres[$field]['$lte'] = $x;
return($this);
}
/**
- where ... $field >= $x AND $field <= $y .. 条件查询判断
-
- @usage = $this->mongo_db->where_between('foo',20,30)->get('foobar');
-
*/
public function where_between($field = '',$x,$y) {
$this->where_init($field);
$this->wheres[$field]['$gte'] = $x;
$this->wheres[$field]['$lte'] = $y;
return($this);
}
/**
- where ... $field > $x AND $field < $y .. 条件查询判断
-
- @usage = $this->mongo_db->where_between_ne('foo',30)->get('foobar');
-
*/
public function where_between_ne($field = '',$y) {
$this->where_init($field);
$this->wheres[$field]['$gt'] = $x;
$this->wheres[$field]['$lt'] = $y;
return($this);
}
/**
- where ... $field <> $x .. 条件查询判断
-
- @usage = $this->mongo_db->where_ne('foo',20)->get('foobar');
-
*/
public function where_ne($field = '',$x) {
$this->where_init($field);
$this->wheres[$field]['$ne'] = $x;
return($this);
}
/**
- where ... or .. 条件查询判断
-
- @usage = $this->mongo_db->where_or('foo',array('foo','bar'))->get('foobar');
-
*/
public function where_or($field = '',$values) {
$this->where_init($field);
$this->wheres[$field]['$or'] = $values;
return($this);
}
/**
- where ... and .. 条件查询判断
-
- @usage = $this->mongo_db->where_and( array ( 'foo' => 1,'b' => 'someexample' );
*/
public function where_and( $elements_values = array() ) {
foreach ( $elements_values as $element => $val ) {
$this->wheres[$element] = $val;
}
return($this);
}
/**
- where $field % $num = $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条件查询(PHP中定义MongoRegex类实现)
-
- @usage : $this->mongo_db->like('foo','bar','im',false,false)->get();
*/
public function like($field = "",$value = "",$flags = "i",$enable_start_wildcard = true,$enable_end_wildcard = true) {
$field = (string)$field;
$this->where_init($field);
$value = (string)$value;
$value = quotmeta($value);
if (true !== $enable_start_wildcard) {
$value = "^".$value;
}
if (true !== $enable_end_wildcard) {
$value .= "$";
}
$regex = "/$value/$flags";
$this->wheres[$field] = new MongoRegex($regex);
return($this);
}
/**
|