欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-09-04 11:43:05
本文实例讲述了php操作mongodb封装类与用法。分享给大家供大家参考,具体如下: 近来学习了mongodb,刚好是做php开发的,随便写了php操作mongodb的封...

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

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

<?php
/**
 * created by phpstorm.
 * user: lee
 * date: 2016/10/24
 * time: 13:49
 */
namespace app\http\controllers\api;
use app\http\common\returnapi;
/*
 *
 *
 mongdb常规操作
 */
class mongdbcommoncontroller
{
  private static $conn;
  private static $mon;
  private static $error;
  private function __construct(){
    //self::$conn = new \mongoclient("mongodb://".env('mongdb_user').":".env('mongdb_pass')."@".env('mongdb_host').":".env('mongdb_port')."/".env('mongdb_db'));
    self::$conn = new \mongoclient("mongodb://".env('mongdb_user').":".env('mongdb_pass')."@".env('mongdb_host').":".env('mongdb_port'));
    //self::$conn = new \mongoclient("mongodb://".env('mongdb_host').":".env('mongdb_port'));
    $db = env('mongdb_db');
    self::$mon = self::$conn->$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, $new_arr, $options);
      return true;
    }
    catch (mongocursorexception $e)
    {
      self::$error = $e->getmessage();
      return false;
    }
  }
  //删除
  public function remove($table, $condition, $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, $condition, $fields=array())
  {
    return self::$mon->$table->findone($condition, $fields);
  }
  //返回表的记录数
  public function count($table)
  {
    return self::$mon->$table->count();
  }
  //返回错误信息
  public function geterror()
  {
    return self::$error;
  }
}

操作实例:

use app\http\controllers\api\mongdbcommoncontroller;
$db = mongdbcommoncontroller::getinstance();
$collection = 'tab';
$data = array('tt' =>'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 '<pre>';
print_r( $db->find($collection, array("tt"=>'dfdfd'), array("start"=>1,"limit"=>4)));
//删除
$db->remove($collection, array('tt' =>'sdsd'));

更多关于php相关内容感兴趣的读者可查看本站专题:《php+mongodb数据库操作技巧大全》、《php基于pdo操作数据库技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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