php实现的mongodb操作类
mongo_db.php
<?php
/**
*CreatedbyPhpStorm.
*User:yangyulong
*Date:2015/5/26
*Time:13:45
*/
classMongo_db
{
privatestatic$instanceof=NULL;
public$mongo;
private$host='localhost';
private$port='27017';
private$db;
public$dbname;
private$table=NULL;
/**
*初始化类,得到mongo的实例对象
*/
publicfunction__construct($host=NULL,$port=NULL,$dbname=NULL,$table=NULL)
{
if(NULL===$dbname){
$this->throwError('集合不能为空!');
}
//判断是否传递了host和port
if(NULL!==$host){
$this->host=$host;
}
if(NULL!==$port){
$this->port=$port;
}
$this->table=$table;
$this->mongo=newMongoClient($this->host.':'.$this->port);
if($this->getVersion()>='0.9.0'){
$this->dbname=$this->mongo->selectDB($dbname);
$this->db=$this->dbname->selectCollection($table);
}else{
$this->db=$this->mongo->$dbname->$table;
}
}
publicfunctiongetVersion()
{
returnMongoClient::VERSION;
}
/**
*单例模式
*@returnMongo|null
*/
//publicstaticfunctiongetInstance($host=null,$port=null,$dbname=null,$table=null){
//
//if(!(self::$instanceofinstanceofself)){
//self::$instanceof=newself($host,$port,$dbname,$table);
//}
//
//returnself::$instanceof;
//}
/**
*插入一条数据
*@paramarray$doc
*/
publicfunctioninsert($doc=array())
{
if(empty($doc)){
$this->throwError('插入的数据不能为空!');
}
//保存数据信息
try{
if(!$this->db->insert($doc)){
thrownewMongoException('插入数据失败');
}
}catch(MongoException$e){
$this->throwError($e->getMessage());
}
}
/**
*插入多条数据信息
*@paramarray$doc
*/
publicfunctioninsertMulti($doc=array())
{
if(empty($doc)){
$this->throwError('插入的数据不能为空!');
}
//插入数据信息
foreach($docas$key=>$val){
//判断$val是不是数组
if(is_array($val)){
$this->insert($val);
}
}
}
/**
*查找一条记录
*@returnarray|null
*/
publicfunctionfindOne($where=NULL)
{
if(NULL===$where){
try{
if($result=$this->db->findOne()){
return$result;
}else{
thrownewMongoException('查找数据失败');
}
}catch(MongoException$e){
$this->throwError($e->getMessage());
}
}else{
try{
if($result=$this->db->findOne($where)){
return$result;
}else{
thrownewMongoException('查找数据失败');
}
}catch(MongoException$e){
$this->throwError($e->getMessage());
}
}
}
/**
*todo带条件的随后做
*查找所有的文档
*@returnMongoCursor
*/
publicfunctionfind($where=NULL)
{
if(NULL===$where){
try{
if($result=$this->db->find()){
}else{
thrownewMongoException('查找数据失败');
}
}catch(MongoException$e){
$this->throwError($e->getMessage());
}
}else{
try{
if($result=$this->db->find($where)){
}else{
thrownewMongoException('查找数据失败');
}
}catch(MongoException$e){
$this->throwError($e->getMessage());
}
}
$arr=array();
foreach($resultas$id=>$val){
$arr[]=$val;
}
return$arr;
}
/**
*获取记录条数
*@returnint
*/
publicfunctiongetCount()
{
try{
if($count=$this->db->count()){
return$count;
}else{
thrownewMongoException('查找总数失败');
}
}catch(MongoException$e){
$this->throwError($e->getMessage());
}
}
/**
*获取所有的数据库
*@returnarray
*/
publicfunctiongetDbs()
{
return$this->mongo->listDBs();
}
/**
*删除数据库
*@paramnull$dbname
*@returnmixed
*/
publicfunctiondropDb($dbname=NULL)
{
if(NULL!==$dbname){
$retult=$this->mongo->dropDB($dbname);
if($retult['ok']){
returnTRUE;
}else{
returnFALSE;
}
}
$this->throwError('请输入要删除的数据库名称');
}
/**
*强制关闭数据库的链接
*/
publicfunctioncloseDb()
{
$this->mongo->close(TRUE);
}
/**
*输出错误信息
*@param$errorInfo错误内容
*/
publicfunctionthrowError($errorInfo='')
{
echo"<h3>出错了:$errorInfo</h3>";
die();
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。