<?php
/**
* php mongodb 操作类,主要用于存取图片
*
* @package MongoDriver
* @version $id$
* @copyright 2015
* @author Lenix
* @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
*/


class MongoDriver
{
protected $connection;//连接
protected $db;//数据库
protected $collection;//集合

/**
* __construct
* 连接数据库,并创建或选择数据库与集合
*
* @param string $server
* @param string $db
* @param string $coll
* @access public
* @return void
* @example $server:mongodb://192.168.6.76:27017
*/
public function __construct($server,$db,$coll)
{
try {
$this->connection=new MongoClient($server);//链接到远程服务器,使用自定义的端口
} catch (MongoConnectionException $e) {
return $e->getMessage();
}
$this->db = $this->connection->$db; //创建 or 选择数据库
$this->collection = $this->db->$coll;//选择创建集合
}

/**
* insert 插入一条数据
*
* @param array $data
* @access public
* @return void
*/
public function insert(array $data)
{
$this->collection->insert($data);
}

/**
* insertBin 把二进制数据插入数据库
*
* @param array $param 查询条件数组
* @param bin $data
* @access public
* @return void
* [example]
* $data=file_get_contents("images/photo.jpg");
* $param = array(
"filename" => "hello.jpg",
"field" => "pic",//字段名
);
[/example]
*/
public function insertBin(array $param=array(),$data=null)
{
$this->collection && $count=$this->collection->count($param);
if(empty($count) && $this->collection){
$field=$param['field'];
$param[$field]=new MongoBinData($data, MongoBinData::GENERIC);
try{
$this->collection->save($param);
}
catch(MongoException $e) {
echo $e->getMessage();
}
catch(MongoCursorException $e) {
echo $e->getMessage();
}
catch(MongoCursorTimeoutException $e) {
echo $e->getMessage();
}
}else{
return 'the file already exists';//不能重复插入同名文件
}

}

/**
* findOne 获取单条记录
*
* @param string $field 字段名
* @access public
* @return void
*/
public function findOne($field)
{
$document = $this->collection->findOne();
if($document) {
return $document[$field]->bin;//输出二进制
}else {
return false;
}
}

/**
* find 获取多条记录
*
* @access public
* @return array
*/
public function find()
{
$cursor=$this->collection->find();
$v=[];
foreach ( $cursor as $id => $value )
{
$v[$id]=$value;

}
return $v;
}

/**
* count 统计集合记录数
*
* @param array $query 查询条件
* @access public
* @return void
* @example
*/
public function count(array $query=array())
{
return $this->collection->count($query);
}

/**
* query 条件查询
*
* @param array $query 查询条件
* @access public
* @return void
* @example $query = array( 'filename' => 'hello.jpg','field'=>'pic' );
*/
public function query(array $query)
{
$field=$query['field'];
if($this->collection) {
$cursor = $this->collection->find($query);
$v=[];

while ( $cursor->hasNext() )
{
$v[]=$cursor->getNext()[$field]->bin;
}
return $v;
}

}
/**
* remove 条件删除
*
* @param array $query
* @access public
* @return array
* @example $query = array( 'filename' => 'hello.jpg' );
*/
public function remove(array $query)
{
return $this->collection->remove($query);
}

/**
* drop 删除集合
*
* @access public
* @return void
*/
public function drop()
{
$this->collection->drop();
}
/**
* addIndex 添加索引
*
* @param array $keys
* @access public
* @return void
* @example $keys=array('x' => 1);
*/
public function addIndex(array $keys)
{
$this->collection->createIndex($keys);

}

/**
* __destruct 关闭连接
*
* @access protected
* @return void
*/
function __destruct()
{
//$this->connection->close($this->collection);//此行一般不需要
$this->connection=null;
$this->collection=null;
$this->db=null;
}

}

?>

 

用法

<?php
include 'mongodb.class.php';

//使用方法

$mongo=new MongoDriver('mongodb://192.168.6.76:27017','wenestthumb','thumb');

//insert
$data=file_get_contents("images/photo.jpg");
$profile = array(
"filename" => "phpto.jpg",
"field" => "pic",
);
//$mongo->insertBin($profile,$data);
$abc=$mongo->find();
var_dump($abc);
exit;

//output
//header('Content-Type: image/jpg');
//$one= $mongo->findOne('pic');
//echo $one;
$query = array( 'filename' => 'phpto.jpg',
'field'=>'pic',
);
$query22 = array( 'filenameaa' => 'phptsfao.jpg',
);

$af=$mongo->remove($query22);
var_dump($af);

//echo $mongo->count(array()); exit;
$pics=$mongo->query($query);
echo $pics[0];

?>

 

来源 http://blog.p2hp.com/archives/1934

最后更新于 2015年9月14日

php mongodb 操作类
标签: