php实例化mongodb,php操作mongodb封装类与用法实例

搜索热词

本文实例讲述了PHP操作mongodb封装类与用法。分享给大家供大家参考,具体如下:

近来学习了mongodb,刚好是做PHP开发的,随便写了PHP操作mongodb的封装类.

PHP;">

$db;

}

public static function getInstance(){

if(!(self::$conn instanceof self)){

self::$conn = new self();

}

//return self::$conn->mydb;

return self::$conn;

}

private function __clone(){

trigger_error('Clone is not allowed');

}//禁止克隆

//创建索引

public function ensureIndex($table,$index,$index_param=array())

{

$index_param['safe'] = 1;

try {

self::$mon->$table->ensureIndex($index,$index_param);

return true;

}

catch (MongoCursorException $e)

{

self::$error = $e->getMessage();

return false;

}

}

//添加

public function insert($table,$arr){

try {

self::$mon->$table->insert($arr,array('w'=>true));

return true;

}

catch (MongoCursorException $e)

{

self::$error = $e->getMessage();

return false;

}

}

//更新

public function update($table,$condition,$new_arr,$options=array())

{

$options['w'] = 1;

if (!isset($options['multiple']))

{

$options['multiple'] = 0;

}

try {

self::$mon->$table->update($condition,$options);

return true;

}

catch (MongoCursorException $e)

{

self::$error = $e->getMessage();

return false;

}

}

//删除

public function remove($table,$options=array())

{

$options['w'] = 1;

try {

self::$mon->$table->remove($condition,$options);

return true;

}

catch (MongoCursorException $e)

{

self::$error = $e->getMessage();

return false;

}

}

//查找

public function find($table,$query_condition,$result_condition=array(),$fields=array())

{

$cursor = self::$mon->$table->find($query_condition,$fields);

if (!empty($result_condition['start']))

{

$cursor->skip($result_condition['start']);

}

if (!empty($result_condition['limit']))

{

$cursor->limit($result_condition['limit']);

}

if (!empty($result_condition['sort']))

{

$cursor->sort($result_condition['sort']);

}

$result = array();

try {

while ($cursor->hasNext())

{

$result[] = $cursor->getNext();

}

}

catch (MongoCursorTimeoutException $e)

{

self::$error = $e->getMessage();

return false;

}

return $result;

}

//查找一条记录

public function findOne($table,$fields=array())

{

return self::$mon->$table->findOne($condition,$fields);

}

//返回表的记录数

public function count($table)

{

return self::$mon->$table->count();

}

//返回错误信息

public function getError()

{

return self::$error;

}

}

操作实例:

'sdsd','pp' => 'ssdsdf');

//返回记录数

echo $db->count($collection);

//插入记录

$db->insert($collection,array("id"=>2,"title"=>"asdqw"));

//更新

$db->update($collection,array("id"=>2),array('tt'=>'dfdfd',"gg"=>"bbb",'hh'=>'dfsdsd'));

//查找记录

echo '';

print_r( $db->find($collection,array("tt"=>'dfdfd'),array("start"=>1,"limit"=>4)));

//删除

$db->remove($collection,array('tt' =>'sdsd'));

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》

希望本文所述对大家PHP程序设计有所帮助。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值