php操作mongodb封装类与用法实例
发布:smiling 来源: PHP粉丝网 添加日期:2021-10-27 15:08:39 浏览: 评论:0
这篇文章主要介绍了php操作mongodb封装类与用法,结合具体实例形式分析了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 (!emptyempty($result_condition['start']))
- {
- $cursor->skip($result_condition['start']);
- }
- if (!emptyempty($result_condition['limit']))
- {
- $cursor->limit($result_condition['limit']);
- }
- if (!emptyempty($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'));
Tags: php操作mongodb
- 上一篇:PHP封装的验证码工具类定义与用法示例
- 下一篇:PHP时间处理类操作示例
相关文章
- ·php操作MongoDB类实例(2021-05-28)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)