Before the cloud Habitat community has sent a few similar articles, we can refer to.
Class Newmongodb {private $mongo;
Newmongodb connection Private $curr _db_name;
Private $curr _table_name;
Private $error;
public $config;
Public Function getinstance ($mongo _server, $flag =array ()) {static $NewMongodb _arr; if (Empty ($flag [' tag ']) {$flag [' tag '] = ' default ';
} if (Isset ($flag [' Force ']) && $flag [' force '] = = true) {$mongo = new Newmongodb ($mongo _server);
if (Empty ($NewMongodb _arr[$flag [' tag ']]) {$NewMongodb _arr[$flag [' tag ']] = $mongo;
return $mongo; else if (isset ($NewMongodb _arr[$flag [' tag ']]) && is_resource ($NewMongodb _arr[$flag [' tag ']]) {RET
Urn $NewMongodb _arr[$flag [' tag ']];
else {$mongo = new Newmongodb ($mongo _server);
$NewMongodb _arr[$flag [' tag ']] = $mongo;
return $mongo; }/** * constructor * supports incoming multiple mongo_server (1.) Connect to other server 2 when a problem occurs. Automatically distribute queries evenly to different servers * * Parameters: * $mongo _server: Number Group or string-array ("127.0.0.1:1111 "," 127.0.0.1:2222 ")-" 127.0.0.1:1111 "* $connect: Connect when initializing MONGO object, default connection * $auto _balance: Automatic load Balancing, default is * * Return Value: * Success: MONGO Object * failed: false */Public function __construct ($mongo _server, $connect =true, $auto _balance=
True) {if (Is_array ($mongo _server)) {$mongo _server_num = count ($mongo _server);
if ($mongo _server_num > 1 && $auto _balance) {$prior _server_num = rand (1, $mongo _server_num);
$rand _keys = Array_rand ($mongo _server, $mongo _server_num);
$mongo _server_str = $mongo _server[$prior _server_num-1]; foreach ($rand _keys as $key) {if ($key!= $prior _server_num-1) {$mongo _server_str. = ', '. $mongo _ser
ver[$key];
}} else {$mongo _server_str = implode (', ', $mongo _server);
} else {$mongo _server_str = $mongo _server;
try {$this->mongo = new Mongoclient ($mongo _server, Array (' Connect ' => $connect)); catch (Mongoconnectionexception $e) {$this->error = $e->getmessage ();
return false;
}/** * Connection NEWMONGODB Server * * Parameter: NO * * return Value: * Success: TRUE * failed: false/Public Function connect ()
{try {$this->mongo->connect ();
return true;
The catch (Mongoconnectionexception $e) {$this->error = $e->getmessage ();
return false; }/** * Select DB * * Parameters: $dbname * * return value: no */Public function Selectdb ($dbname) {$this->CU
Rr_db_name = $dbname;
/** * CREATE INDEX: If index already exists, return. * * Parameters: * $table _name: Table name * $index: Index-array ("id" =>1)-establish ascending index in ID field * $index _param: Other conditions-Unique index etc * Return value: * Successful : True * Failed: false */Public function ensureindex ($table _name, $index, $index _param=array ()) {$dbname = $this-&
Gt;curr_db_name;
$index _param[' safe '] = 1;
try {$this->mongo-> $dbname-> $table _name->ensureindex ($index, $index _param);
return true; catch (mongocursOrexception $e) {$this->error = $e->getmessage ();
return false; /** * Insert Record * * Parameters: * $table _name: Table name * $record: Record * * return value: * Success: TRUE * failed: false/Public Fun
ction Insert ($table _name, $record) {$dbname = $this->curr_db_name;
try {$this->mongo-> $dbname-> $table _name->insert ($record, Array (' safe ' =>true));
return true;
The catch (Mongocursorexception $e) {$this->error = $e->getmessage ();
return false;
/** * Query table number of records * * Parameters: * $table _name: Table name * * Return value: Table number of records/Public function count ($table _name) {
$dbname = $this->curr_db_name;
return $this->mongo-> $dbname-> $table _name->count (); /** * Update Record * * Parameters: * $table _name: Table name * $condition: Update condition * $newdata: New data record * $options: Update selection-UPSERT/MULTIPL E * * Return value: * Successful: TRUE * failed: false */Public Function update ($table _name, $condition, $newdata, $options =arraY ()) {$dbname = $this->curr_db_name;
$options [' safe '] = 1; if (!isset ($options [' multiple ']) {$options [' multiple '] = 0;
try {$this->mongo-> $dbname-> $table _name->update ($condition, $newdata, $options);
return true;
The catch (Mongocursorexception $e) {$this->error = $e->getmessage ();
return false; /** * Delete Record * * Parameters: * $table _name: Table name * $condition: Delete condition * $options: Delete selection-justone * * return Value: * Success: TR UE * FAILED: false/Public Function remove ($table _name, $condition, $options =array ()) {$dbname = $this->curr_
db_name;
$options [' safe '] = 1;
try {$this->mongo-> $dbname-> $table _name->remove ($condition, $options);
return true;
The catch (Mongocursorexception $e) {$this->error = $e->getmessage ();
return false; /** * Find Records * * Parameters: * $table _name: Table name * $query _condition: Field Lookup criteria * $result _condition: Query result restriction-limit/sort etc * $fields: Get field * * return value: * Success: Recordset * failed: false/Public Function find ($table _name, $
Query_condition, $result _condition=array (), $fields =array ()) {$dbname = $this->curr_db_name;
$cursor = $this->mongo-> $dbname-> $table _name->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 ();
The catch (Mongoconnectionexception $e) {$this->error = $e->getmessage ();
return false;
The catch (Mongocursortimeoutexception $e) {$this->error = $e->getmessage (); Return False
return $result; /** * Find a record * * Parameters: * $table _name: Table name * $condition: Find condition * $fields: Get field * * return value: * Success: One record * failed: F
Alse */Public Function FindOne ($table _name, $condition, $fields =array ()) {$dbname = $this->curr_db_name;
return $this->mongo-> $dbname-> $table _name->findone ($condition, $fields);
/** * Get current error message * * Parameter: NO * * return value: Current error message/Public function GetError () {returns $this->error;
/*** NEWMONGODB Class * * Examples: * $mongo = new Newmongodb ("127.0.0.1:11223");
* $mongo->selectdb ("test_db");
* CREATE INDEX * $mongo->ensureindex ("test_table", Array ("id" =>1), Array (' unique ' =>true));
* Get the record of the table * $mongo->count ("test_table");
* Insert Record * $mongo->insert ("test_table", Array ("id" =>2, "title" => "Asdqw"));
* Update record * $mongo->update ("test_table", Array ("id" =>1), Array ("id" =>1, "title" => "BBB")); * Update records-when they are present and added when they are not present-equivalent to set * $mongo->update ("Test_tabLe ", Array (" id "=>1), Array (" id "=>1," title "=>" BBB "), Array (" Upsert "=>1)); * Find Records * $mongo->find ("C", Array ("title" => "Asdqw"), Array ("Start" =>2, "Limit" =>2, "Sort" =>array ("id" =
>1)) * Find a record * $mongo->findone ("$mongo->findone (" TTT ", Array (" id "=>1))", Array ("id" =>1));
* Delete Record * $mongo->remove ("TTT", Array ("title" => "BBB"));
* Delete Only one record * $mongo->remove ("TTT", Array ("title" => "BBB"), Array ("Justone" =>1));
* Get the error message for the MONGO operation * $mongo->geterror (); */
}