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;
/** * Initializes the class, obtains the MONGO instance object/Public function __construct ($host = null, $port = NULL, $dbname = NULL, $table = NULL) {if (NULL = = $dbname) {$this->throwerror (' collection cannot be empty!
');
}//Judge whether the host and port if (NULL!== $host) {$this->host = $host are passed;
} 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;
The Public Function getversion () {return mongoclient::version; /** * Single case mode * @return mongo|null///public static function getinstance ($host =null, $port =null, $dbname =n ull, $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 (' inserted data cannot be empty!
');
///Save data information try {if (! $this->db->insert ($doc)) {throw new Mongoexception (' Insert data failed ');
The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); /** * Inserts 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) {//To determine if $val is not 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 (' Find data failed ');
The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());
} else {try {if ($result = $this->db->findone ($where)) {return $result;
else {throw new mongoexception (' Find data failed ');
The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());
/** * TODO with conditional subsequent * Find all documents * @return Mongocursor */Public Function found ($where = NULL)
{if (NULL = = $where) {try {if ($result = $this->db->find ()) {} else { throw new Mongoexception (' Find data failed ');
The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); } else {try {if ($result = $this->db->find ($where)) {} else {throw new M
Ongoexception (' Find data failed ');
The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());
}} $arr = Array ();
foreach ($result as $id => $val) {$arr [] = $val;
return $arr; /** * Gets the number of record bars * @return int/Public Function GetCount () {try {if ($count = $this->DB-&G
T;count ()) {return $count;
else {throw new mongoexception (' Find total failed ');
The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); /** * Get all database * @return Array/Public function Getdbs () {return $this->mongo->listdbs
();
/** * Delete database * @param null $dbname * @return Mixed */Public function dropdb ($dbname = null) {if (null!== $dbname) {$retult = $this-&G
T;mongo->dropdb ($dbname);
if ($retult [' OK ']) {return TRUE;
else {return FALSE;
}} $this->throwerror (' Enter the name of the database to be deleted ');
/** * Forces the link to close the database/Public function closedb () {$this->mongo->close (TRUE); /** * Output error message * @param $errorInfo error content/Public Function throwerror ($errorInfo = ') {echo "<H3&G
t; a mistake: $errorInfo
The above is the entire contents of this article, I hope you can enjoy.