A complete example of the MONGODB database operation class implemented by PHP

Source: Internet
Author: User
Tags findone mongodb server php framework
This article mainly introduces the implementation of the MONGODB database operation class PHP, combined with a complete example of the form of PHP based on a single model for MongoDB database connection, increase deletion check, statistics and other operations related implementation skills, the need for friends can refer to the next

This paper describes the MongoDB database operation class implemented by PHP. Share to everyone for your reference, as follows:

Recent project development using the database is the MongoDB database, because the small part of the company is also just using MongoDB database, so there is no encapsulation of the MONGODB database operation class to use, so small in the project itself encapsulated a MONGODB database operation class, Special take out to share, the unsatisfactory place wants everybody not to spray.

As we all know, MongoDB is a typical NoSQL database representative, by many developers, in recent years, especially hot, mongodb is not the prevalence of no reason, below for everyone to briefly introduce the next MongoDB.

MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-relational database. The data structure he supports is very loose and is a JSON-like Bjson format, so you can store more complex data types. MONGO's biggest feature is that the query language he supports is very powerful, and its syntax is a bit like an object-oriented query language that almost implements most of the functionality of a relational database single-table query, and also supports indexing of data.

It is characterized by high performance, easy to deploy, easy to use, and easy to store data. The main features are:

For collection storage, easy to store data for object types.
Mode of freedom.
Supports dynamic queries.
Supports full indexes, including internal objects.
Support Queries.
Supports replication and recovery.
Use efficient binary data storage, including large objects such as video.
Automatically process fragmentation to support scalability at the cloud level
Supports multiple languages such as ruby,python,java,c++,php.
File storage format is Bson (an extension of JSON)
Accessible over the network

The so-called "set-oriented" (collenction-orented), meaning that data is grouped in a dataset, is called a collection (collenction). Each collection has a unique identifying 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), unlike it does not need to define any schema (schema).

Mode Freedom (schema-free) means that for files stored in a MongoDB database, we do not need to know any of its structure definitions. If necessary, you can store files of different structures in the same database.

The documents stored in the collection are stored in the form of key-value pairs. The key is used to uniquely identify a document as a string type, whereas a 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. Recommended to run on 64-bit platforms because MongoDB

The maximum file size supported when running in 32-bit mode is 2GB.

MongoDB stores the data in a file (the default path is:/data/db), which is managed using a memory-mapped file for increased efficiency.

Small series of their own encapsulated PHP operation MongoDB Database Database operation class source code is as follows, for reference only.

<?php/** * PHP Operation MongoDB Database operation class */class databases {protected $database = ';  protected $mo;    /** * Construction 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 */publi    C 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 * @para M 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 Specifies 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; /** * Count * @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 MONGO command directly * @param $sql * @return Array */Public function Toexcute ($sql) {$result = $this->dat    Abase->execute ($sql);  return $result; /** * Group Statistics * @param $table * @param $where * @param $field */Public Function GroupCount ($table, $where, $          field) {$cond = 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 ($tabl    E, $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, $qu ery = 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 ']; }}?>

Articles you may be interested in:

thinkphp Framework uses redirect to implement page redirection Method Example explained

A variety of ways to include a specified string in a PHP string

PHP Framework CodeIgniter uses Redis to explain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.