版权声明:转载请注明出处 https://blog.csdn.net/weixin_40325128/article/details/84235770
第一次使用mongo的时候给我的感觉就是文档对象处理起来麻烦,查询语法拼接不够平滑好记,因为处理惯了数组,所以这里基于尝试着以tp的方式封装了一个类库,方便使用
调用方式都在最下面已经注释掉的部分,快来试试吧
<?php
/**
* mongodb驱动类
*/
class MongoDriver
{
private static $db;
private static $currentCollection;
private $option = array(
'condition'=>array(),
'field'=>array('_id'=>false),
'sort'=>array(),
'limitNum'=>null,
'offset'=>null
);
private static $_config = array(
'host'=>'127.0.0.1',
'port'=>'27017',
'dbName'=>'test',
'user'=>'wujia',
'pass'=>'wujia'
);
public function __construct()
{
$this->connect();
}
/**
* [connect 连接方法]
* @param array $connectInfo [连接信息]
*/
public function connect($connectInfo=array())
{
if(!$connectInfo)
{
$connectInfo = self::$_config;
}
$connectLink ="mongodb://".$connectInfo['host'].":".$connectInfo['port'];
$mongo = new MongoClient($connectLink);
// $mongo = new MongoDB\Driver\Manager($connectLink);
$db = $mongo->$connectInfo['dbName'];
$db->authenticate($connectInfo['user'], $connectInfo['pass']);
self::$db = $db;
}
/**
* [collection 选择集合]
* @param [string] $collectionName [集合名称]
*/
public function collection($collectionName)
{
if($collectionName&&strlen($collectionName)>0)
{
self::$currentCollection = self::$db->$collectionName;
}
}
/**
* [variableInit option内参数初始化]
*/
private function variableInit()
{
$this->option =array(
'condition'=>array(),
'field'=>array('_id'=>false),
'sort'=>array(),
'limitNum'=>null,
'offset'=>null
);
}
/**
* [where 条件组织方法]
* @param array/string $condition [筛选条件,支持字符串/数组]
*/
public function where($condition)
{
//数字形式且不为空
if(count($condition)>0)
{
$this->option['condition'] = $condition;
}
//字符串形式
if(is_string($condition)&&strlen($condition)>0)
{
$cond = array();
//小写
$condition = strtolower($condition);
$condition = explode('and',$condition);
//and 条件组织
foreach($condition as $v)
{
list($key,$value) = explode('=',$v);
$key = trim($key);
$value = trim($value);
//value是数字
if(is_numeric($value))
{
if(strpos($value,'.')>0)
{
//浮点型
$value = floatval($value);
}
else
{
//整形
$value = intval($value);
}
}
//value是字符串,去掉''
if(is_string($value))
{
$value = trim($value,"'");
}
$cond[$key] = $value;
}
$this->option['condition'] = $cond;
}
return $this;
}
/**
* [field 返回字段组织方法]
* @param [array/string] $fileds [参数列表,支持字符串或数组]
*/
public function field($fileds)
{
$fieldArr = array();
//字符串形式
if(is_string($fileds)&&strlen($fileds)>0)
{
$fileds = strtolower($fileds);
$fileds = explode(',',$fileds);
}
foreach($fileds as $value)
{
$fieldArr[$value]=true;
}
$this->option['field'] = array_merge($this->option['field'],$fieldArr);
return $this;
}
/**
* [add 数据写入方法]
* @param [array] $data [新增数据数组]
*/
public function add($data)
{
if(is_array($data)&&count($data)>0)
{
return self::$currentCollection->insert($data);
}
}
/**
* [select 多条数据查询方法]
* @return [array] [查询结果]
*/
public function select()
{
$result = self::$currentCollection
->find($this->option['condition'],$this->option['field'])
->sort($this->option['sort'])
->limit($this->option['limitNum'])
->skip($this->option['offset']);
$returnArr = array();
foreach($result as $key => $value)
{
$returnArr[]=$value;
}
$this->variableInit();
return $returnArr;
}
/**
* [find 获取单条记录方法]
* @return [array] [查询结果]
*/
public function find()
{
$result = self::$currentCollection->findOne($this->option['condition'],$this->option['field']);
$this->variableInit();
return $result;
}
/**
* [save 数据更新方法]
* @param [array] $data [更新字段数组]
* @return [boolean] [是否更新成功]
*/
public function save($data)
{
$update = self::$currentCollection->update($this->option['condition'],array('$set'=>$data));
$this->variableInit();
$re = $update['nModified']>0?true:false;
return $re;
}
/**
* [del 删除方法]
* @param boolean $limitOne [是否只删除一条]
* @return [type] [删除结果]
*/
public function del($limitOne=false)
{
$justOne =false;
if($limitOne == 1)
{
$justOne = true;
}
$re = self::$currentCollection->remove($this->option['condition'],array('justOne'=>$justOne));
$this->variableInit();
$re = $re['n']>0?true:false;
return $re;
}
/**
* [order 排序方法]
* @param array $rules [排序规则]
* @return [type] [description]
*/
public function order($rules = array())
{
if(is_string($rules)&&strlen($rules)>0)
{
$rule = explode(',',strtolower($rules));
$rulesArr = array();
foreach($rule as $key => $value)
{
list($field,$sort)=explode(' ',$value);
$sorted = $sort=='desc'?-1:1;
$rulesArr[$field] = $sorted;
}
$this->option['sort'] = $rulesArr;
}
if(is_array($rules)&&count($rules)>0)
{
$this->option['sort'] = $rules;
}
return $this;
}
/**
* [limit 分页方法]
* @param [integer] $offset [偏移量]
* @param [integer] $limitNum [查询数量]
*/
public function limit($offset,$limitNum=null)
{
$this->option['limitNum'] = is_null($limitNum)?$offset:$limitNum;
$this->option['offset'] = is_null($limitNum)?0:$offset;
return $this;
}
}
//实例化
$mongodb = new MongoDriver();
//选择集合
$mongodb->collection('user');
/*
添加数据
$data = array(
'name'=>'zhenghongying1',
'age'=>49
);
$re = $mongodb->add($data);
if($re['ok']==1)
{
echo '插入成功';
}
*/
/*
查询多条数据
$re = $mongodb->select();
$re = $mongodb->field("name,age")->where(array('name'=>'zhenghongying1'))->order(array("age"=>-1))->limit(2)->select();
查询1条数据
$re = $mongodb->field("name,age")->where(array('name'=>'zhenghongying1','age'=>49))->find();
*/
/*
更新
$where = array("name"=>'zhenghongying1');
$data = array("age"=>19);
$re =$mongodb->where($where)->save($data);
*/
/*
删除
$re = $mongodb->where("name='zhenghongying1'")->del(1);
*/
$re = $mongodb
->field("name,age")
->where(array('name'=>'zhenghongying1'))
->order(array("age"=>-1))
->limit(2)
->select();
var_dump($re);