php操作mongodb封裝類與用法實例
本文實例講述了php操作mongodb封裝類與用法。分享給大家供大家參考,具體如下:
近來學(xué)習(xí)了mongodb,剛好是做php開發(fā)的,隨便寫了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常規(guī)操作
*/
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');
}//禁止克隆
//創(chuàng)建索引
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);
}
//返回表的記錄數(shù)
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');
//返回記錄數(shù)
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'));
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP中error_reporting()函數(shù)的用法(修改PHP屏蔽錯誤)
一般在默認(rèn)的普通PHP文件中輸出一個未定義聲明的變量是不會報錯誤的,但在codeigniter框架下卻要報錯誤,這對于想集成 添加 和 修改 頁面于一體的”懶人”很不方便,由于是初學(xué)者開始還想怎么在代碼中屏蔽這一錯誤提示呢.甚至用到了@,但聽很多人都說@會大大降低性能.2011-07-07
PHP中include()與require()的區(qū)別說明
引用文件的方法有兩種:require 及 include。兩種方式提供不同的使用彈性。2010-03-03
php版本CKEditor 4和CKFinder安裝及配置方法圖文教程
這篇文章主要介紹了php版本CKEditor 4和CKFinder安裝及配置方法,結(jié)合圖文與實例形式詳細分析了php安裝及配置CKEditor 4和CKFinder相關(guān)實現(xiàn)步驟、操作技巧與注意事項,需要的朋友可以參考下2019-06-06
php使用post數(shù)組的鍵值創(chuàng)建同名變量并賦值的方法
這篇文章主要介紹了php使用post數(shù)組的鍵值創(chuàng)建同名變量并賦值的方法,涉及php操作數(shù)組的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
PHP網(wǎng)絡(luò)安全之命令執(zhí)行漏洞及防御
這篇文章主要介紹了PHP命令執(zhí)行漏洞及防御,網(wǎng)絡(luò)安全越來越受重視的今天,漏洞與防御都需要有所了解,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
php生成SessionID和圖片校驗碼的思路和實現(xiàn)代碼
做一個后臺登陸需要用到校驗碼,前臺的用戶跟蹤需要用到SessionID,當(dāng)然,默認(rèn)的PHP開啟了Session以后就有了一個SessionID,但是我需要自己的,并且能夠存儲進數(shù)據(jù)庫,那么我就嘗試了一下,構(gòu)造了以下的函數(shù)。2009-03-03

