This article describes the mongoDB database operation class implemented by PHP. Share it for your reference, as follows:
The database used in recent project development is the mongodb database. Because the editor's company has just used the mongodb database, the mongodb database operation class that has not been encapsulated before is used. Therefore, the editor encapsulated a mongodb database operation class in the project and shared it out. I hope everyone will not criticize the unsatisfactory areas.
As we all know, mongodb is a representative of a typical nosql database and is sought after by many developers. It has been particularly popular in recent years. There is a reason for the popularity of mongodb. Let me briefly introduce MongoDB to you.
MongoDB is a product between a relational database and a non-relational database. It is the most functional and most similar to a relational database among non-relational databases. The data structure it supports is very loose and is in a json-like bjson format, so it can store relatively complex data types. Mongo's biggest feature is that the query language it supports is very powerful. Its syntax is a bit similar to an object-oriented query language. It can almost realize most of the functions similar to single table query of relational databases, and it also supports indexing of data.
Its features are high performance, easy to deploy, easy to use, and very convenient to store data. The main functional features are:
For collection storage, easy to store object type data.
Free mode.
Support dynamic query.
Supports full indexing and contains internal objects.
Support query.
Supports replication and failure recovery.
Use efficient binary data storage, including large objects (such as videos, etc.).
Automatically process fragmentation to support the scalability of the cloud computing level
Supports RUBY, PYTHON, JAVA, C++, PHP and other languages.
File storage format is BSON (an extension of JSON)
Accessible via the network
The so-called "collenction-Orented" means that data is stored in a data set in groups and is called a collection. Each collection has a unique identifier name in the database and can contain an unlimited number of documents. The concept of a collection is similar to a table in a relational database (RDBMS), except that it does not require defining any schema.
Schema-free means that for files stored in mongodb database, we do not need to know any structural definitions of it. If you need it, you can store files of different structures in the same database.
Documents stored in the collection are stored in the form of key-value pairs. The key is used to uniquely identify a document, which is a string type, while the value can be a complex file type in each. We call this storage form BSON (Binary Serialized dOcument Format).
The MongoDB server can run on Linux, Windows or OS X platforms, supports 32-bit and 64-bit applications, and the default port is 27017. It is recommended to run on 64-bit platforms because MongoDB
The maximum file size supported when running in 32-bit mode is 2GB.
MongoDB stores data in files (the default path is: /data/db), and uses memory mapped files for management to improve efficiency.
The source code of the database operation class of the PHP operation MongoDB database that I encapsulated by myself is as follows, for reference only.
<?php /** * PHP operation mongodb database operation class */ class Database { protected $database = ''; protected $mo; /** * Constructing method */ public function __construct() { $server = DBSERVER; $user = DBUSER; $password = DBPASS; $port = DBPORT; $database = DBNAME; $mongo = $this->getInstance($server, $user, $password, $port); $this->database = $mongo->$database; } /** * Database singleton method * @param $server * @param $user * @param $password * @param $port * @return Mongo */ public function getInstance($server, $user, $password, $port) { if (isset($this->mo)) { return $this->mo; } else { if (!empty($server)) { if (!empty($port)) { if (!empty($user) && !empty($password)) { $this->mo = new Mongo("mongodb://{$user}:{$password}@{$server}:{$port}"); } else { $this->mo = new Mongo("mongodb://{$server}:{$port}"); } } else { $this->mo = new Mongo("mongodb://{$server}"); } } else { $this->mo = new Mongo(); } return $this->mo; } } /** * Query all data in the table * @param $table * @param array $where * @param array $sort * @param string $limit * @param string $skip * @return array|int */ public function getAll($table, $where = array(), $sort = array(), $limit = '', $skip = '') { if (!empty($where)) { $data = $this->database->$table->find($where); } else { $data = $this->database->$table->find(); } if (!empty($sort)) { $data = $data->sort($sort); } if (!empty($limit)) { $data = $data->limit($limit); } if (!empty($skip)) { $data = $data->skip($skip); } $newData = array(); while ($data->hasNext()) { $newData[] = $data->getNext(); } if (count($newData) == 0) { return 0; } return $newData; } /** * Query Specify a data * @param $table * @param array $where * @return int */ public function getOne($table, $where = array()) { if (!empty($where)) { $data = $this->database->$table->findOne($where); } else { $data = $this->database->$table->findOne(); } return $data; } /** * Statistics * @param $table * @param array $where * @return mixed */ public function getCount($table, $where = array()) { if (!empty($where)) { $data = $this->database->$table->find($where)->count(); } else { $data = $this->database->$table->find()->count(); } return $data; } /** * Execute the mongo command directly * @param $sql * @return array */ public function toExcute($sql) { $result = $this->database->execute($sql); return $result; } /** * Group count * @param $table * @param $where * @param $field */ public function groupCount($table, $where, $field) { $cond = array( array( '$match' => $where, ), array( '$group' => array( '_id' => '$' . $field, 'count' => array('$sum' => 1), ), ), array( '$sort' => array("count" => -1), ), ); $this->database->$table->aggregate($cond); } /** * Delete data * @param $table * @param $where * @return array|bool */ public function toDelete($table, $where) { $re = $this->database->$table->remove($where); return $re; } /** * Insert data * @param $table * @param $data * @return array|bool */ public function toInsert($table, $data) { $re = $this->database->$table->insert($data); return $re; } /** * Update data * @param $table * @param $where * @param $data * @return bool */ public function toUpdate($table, $where, $data) { $re = $this->database->$table->update($where, array('$set' => $data)); return $re; } /** * Get unique data * @param $table * @param $key * @return array */ public function distinctData($table, $key, $query = array()) { if (!empty($query)) { $where = array('distinct' => $table, 'key' => $key, 'query' => $query); } else { $where = array('distinct' => $table, 'key' => $key); } $data = $this->database->command($where); return $data['values']; } } ?>
For more information about PHP related content, please check out the topic of this site:Complete collection of PHP+MongoDB database operation skills》、《Summary of PHP's skills to operate database based on pdo》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.