本文实例讲述了PHP操作Mongodb封装类。分享给大家供大家参考,具体如下:
'lt', // id > 1
'<=' => 'lte', // id <= 10
'>' => 'gt', // id > 5
'>=' => 'gte', // id >= 4
'!=' => 'ne', // id != 4
'%' => 'mod', // id % 4 = 0
'in' => 'in', // id in (1,2,3,4)
'notin' => 'nin',// id not in (1,2,3,4)
'or' => 'or', // id=1 or id=2
'not' => 'not', // !(id=1)
);
public function __construct($config = array('host' => 'xxx', 'port' => 27017, 'username' => 'xxx', 'password' => 'xxx', 'db' => 'xxx', 'cmd' => '$')){
$server = sprintf("mongodb://%s:%s@%s:%s/%s", $config['username'], $config['password'], $config['host'], $config['port'], $config['db']);
// echo "connectn";
try {
$this->_mongo = new MongoClient($server, array('connect'=>true));// 立即连接
}catch (MongoConnectionException $e){
if(self::DEBUG) {
echo $e->getMessage();
}
return false;
}
$this->selectDB($config['db']);
// 命令前缀
if(!isset($config['cmd'])){
$this->_cmd = ini_get('mongo.cmd');
if($this->_cmd == ''){
$this->_cmd = '$';
}
}
}
public function insert($colName, $sets, $safe=false, $fsync=false){
$col = $this->_getCol($colName);
try {
$col->insert($sets,array('w'=>$safe,'fsync'=>$fsync));
return true;
}catch (MongoCursorException $e){
return false;
}
}
public function save($colName, $sets, $safe=false, $fsync=false){
// 处理 '_id' 字段
$sets = $this->_parseId($sets);
$ret = $this->_getCol($colName)->save($sets,array('w'=>$safe,'fsync'=>$fsync));
return $ret;
}
public function delete($colName,$query=array(),$delAll=true,$safe=false,$fsync=false){
// 自动处理 '_id' 字段
$query = $this->_parseId($query);
// 删除选项
$options = array(
'justOne' => !$delAll,
'w' => $safe,
'fsync' => $fsync,
);
$col = $this->_getCol($colName);
return $col->remove($query,$options);
}
public function dropCol($colName){
return $this->_getCol($colName)->drop();
}
public function update($colName,$newDoc,$query=array(),$option='set',$upAll=true,$upsert=false,$safe=false,$fsync=false){
// 自动处理 '_id' 字段
$query = $this->_parseId($query);
// 得到集合
$col = $this->_getCol($colName);
// 重新组合新文档
if($option != 'replace'){
$newDoc = array($this->cmd($option) => $newDoc);
}
// 更新条件
$options = array(
'upsert' => $upsert,
'multiple' => $upAll,
'w' => $safe,
'fsync' => $fsync,
);
return $col->update($query,$newDoc,$options);
}
public function select($colName,$query=array(),$fields=array(),$sort=array(),$limit=0,$skip=0){
// 得到集合
$col = $this->_getCol($colName);
// 自动处理 '_id' 字段
$query = $this->_parseId($query);
// 结果集偏历
$cursor = $col->find($query,$fields);
// 排序
if($sort){
$cursor->sort($sort);
}
// 跳过记录数
if($skip > 0){
$cursor->skip($skip);
}
// 取多少行记录
if($limit > 0){
$cursor->limit($limit);
}
$result = array();
foreach($cursor as $row){
$result[] = $this->_parseArr($row);
}
return $result;
}
public function count($colName,$query=array(),$limit=0,$skip=0){
return $this->_getCol($colName)->count($query,$limit,$skip);
}
public function fetchRow($colName,$query=array(), $fields=array()){
// 得到集合名
$col = $this->_getCol($colName);
// 自动处理 '_id' 字段
$query = $this->_parseId($query);
// 处理结果集
return $this->_parseArr($col->findOne($query,$fields));
}
public function fetchOne($colName,$query=array(), $fields='_id'){
$ret = $this->fetchRow($colName,$query,array($fields));
return isset($ret[$fields]) ? $ret[$fields] : false;
}
public function fetchCol($colName,$query=array(), $fields='_id'){
$result = array();
$list = $this->select($colName,$query,array($fields));
foreach ($list as $row){
$result[] = $row[$fields];
}
return $result;
}
public function fetchAssoc($colName,$query=array(), $fields='_id'){
$result = array();
$list = $this->select($colName,$query);
foreach ($list as $row){
$key = $row[$fields];
$result[][$key] = $row;
}
return $result;
}
public function cmd($option=''){
// 只返回命令前缀
if($option == ''){
return $this->_cmd;
}
// 如果是操作符
if(isset($this->_condMap[$option])){
$option = $this->_condMap[$option];
}
return $this->_cmd.$option;
}
public function selectDB($dbname){
$this->_db = $this->_mongo->selectDB($dbname);
}
public function allDB($onlyName=false){
$ary = $this->_mongo->listDBs();
if($onlyName){
$ret = array();
foreach ($ary['databases'] as $row){
$ret[] = $row['name'];
}
return $ret;
}else{
return $ary;
}
}
public function dropDB($dbname){
return $this->_mongo->dropDB($dbname);
}
public function close(){
$this->_mongo->close();
}
public function getMongo(){
return $this->_mongo;
}
public function getError(){
return $this->_db->lastError();
}
// 解析数据组中的'_id'字段(如果有的话)
private function _parseId($arr){
if(isset($arr['_id'])){
$arr['_id'] = new MongoId($arr['_id']);
}
return $arr;
}
// 得到集合对象
private function _getCol($colName){
return $this->_db->selectCollection($colName);
}
// 解析数组中的"_id"并且返回
private function _parseArr($arr){
if(!empty($arr)) {
$ret = (array)$arr['_id'];
$arr['_id'] = $ret['$id'];
}
return $arr;
}
}//End Class
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+MongoDB数据库操作技巧大全》、《PHP基于pdo操作数据库技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。



