mongodb operation class implemented by php
Updated: May 28, 2015 11:39:15 Submission: hebedich
Speaking of php and mongoDB, I have to first introduce the official manual of php. The URL is: /manual/en/. Next, I will share with you a commonly used MONGODB operation category. For details, you can refer to the database operations.
mongo_db.php
<?php /** * Created by PhpStorm. * User: yangyulong * Date: 2015/5/26 * Time: 13:45 */ class Mongo_db { private static $instanceof = NULL; public $mongo; private $host = 'localhost'; private $port = '27017'; private $db; public $dbname; private $table = NULL; /** * Initialize the class and get the instance object of mongo */ public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL) { if (NULL === $dbname) { $this->throwError('The set cannot be empty! '); } //Judge whether the host and port are passed if (NULL !== $host) { $this->host = $host; } if (NULL !== $port) { $this->port = $port; } $this->table = $table; $this->mongo = new MongoClient($this->host . ':' . $this->port); if ($this->getVersion() >= '0.9.0') { $this->dbname = $this->mongo->selectDB($dbname); $this->db = $this->dbname->selectCollection($table); } else { $this->db = $this->mongo->$dbname->$table; } } public function getVersion() { return MongoClient::VERSION; } /** * Singleton mode * @return Mongo|null */ //public static function getInstance($host=null, $port=null, $dbname=null, $table=null){ // // if(!(self::$instanceof instanceof self)){ // self::$instanceof = new self($host, $port, $dbname, $table); // } // // return self::$instanceof; //} /** * Insert a data * @param array $doc */ public function insert($doc = array()) { if (empty($doc)) { $this->throwError('The inserted data cannot be empty! '); } //Save data information try { if (!$this->db->insert($doc)) { throw new MongoException('Insert data failed'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } /** * Insert multiple data information * @param array $doc */ public function insertMulti($doc = array()) { if (empty($doc)) { $this->throwError('The inserted data cannot be empty! '); } //Insert data information foreach ($doc as $key => $val) { //Judge whether $val is an array if (is_array($val)) { $this->insert($val); } } } /** * Find a record * @return array|null */ public function findOne($where = NULL) { if (NULL === $where) { try { if ($result = $this->db->findOne()) { return $result; } else { throw new MongoException('Failed to find data'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } else { try { if ($result = $this->db->findOne($where)) { return $result; } else { throw new MongoException('Failed to find data'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } } /** * todo with conditions and then do it * Find all documents * @return MongoCursor */ public function find($where = NULL) { if (NULL === $where) { try { if ($result = $this->db->find()) { } else { throw new MongoException('Failed to find data'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } else { try { if ($result = $this->db->find($where)) { } else { throw new MongoException('Failed to find data'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } $arr = array(); foreach ($result as $id => $val) { $arr[] = $val; } return $arr; } /** * Get the number of records * @return int */ public function getCount() { try { if ($count = $this->db->count()) { return $count; } else { throw new MongoException('Failed to find total number'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } /** * Get all databases * @return array */ public function getDbs() { return $this->mongo->listDBs(); } /** * Delete the database * @param null $dbname * @return mixed */ public function dropDb($dbname = NULL) { if (NULL !== $dbname) { $retult = $this->mongo->dropDB($dbname); if ($retult['ok']) { return TRUE; } else { return FALSE; } } $this->throwError('Please enter the database name to be deleted'); } /** * Forced to close the database link */ public function closeDb() { $this->mongo->close(TRUE); } /** * Output error message * @param $errorInfo Error content */ public function throwError($errorInfo='') { echo "<h3>Error: $errorInfo</h3>"; die(); } }
The above is the entire content of this article, I hope you like it.
Related Articles
Analysis of PHP path blasting problems caused by Windows' file system mechanism
This article mainly introduces the analysis of PHP path blasting problems caused by the Windows file system mechanism. Friends who need it can refer to it2014-07-07Destoon supply information title calls the method of company name
This article mainly introduces the method of calling the company name by destoon supply information title. It is a very practical technique. Friends who need it can refer to it.2014-08-08RASP implementation process analysis in PHP
PHP’s RASP is embedded in the PHP interpreter through the PHP expansion. This article introduces the RASP implementation process analysis and implementation operation code in PHP. Interested friends follow the editor to take a look.2022-02-02Yii Example of implementing the function of article list top
This article mainly introduces Yii to implement the function of the article list, analyzes the implementation steps and related code operation techniques of the article list, friends who need it can refer to it2016-10-10Analysis of the usage of Smarty template variable regulator
This article mainly introduces the usage of Smarty template variable regulators, and analyzes the functions and specific usage techniques of Smarty template variable regulators in a more detailed manner. Friends who need it can refer to it2016-05-05Share the tutorial on using PHP header function
In the php language, the header() function is very useful, especially when using ajax, it will help you solve some unexpected problems. Below are some detailed explanations of the header. Hope it helps phper2013-09-09php smarty truncate UTF8 garbled problem solution
This article mainly introduces the solution to the php smarty truncate UTF8 garbled problem. Friends who need it can refer to it2014-06-06Generate random watermark pictures based on PHP
This article mainly introduces the generation of random watermark images based on PHP. The example code is introduced in this article in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.2020-12-12How to convert html into picture by php
Below, the editor will bring you an article on how to convert html into pictures by php. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor2017-05-05Example of Yii2 framework to implement login and add verification code function
This article mainly introduces the Yii2 framework to implement the login and add verification code function. It analyzes the settings, controller and view operation skills related to the login and add verification code of Yii2 framework to analyze the settings, controller and view operation skills of login and add verification code in Yii2 framework. Friends who need it can refer to it.2018-07-07