<?php
/*** MongoDB Class * * Examples:
* $mongo = new Hmongodb ("127.0.0.1:11223");
* $mongo->selectdb ("test_db");
* Create an index
* $mongo->ensureindex ("test_table", Array ("id" =>1), Array (' unique ' =>true));
* Get Records of tables
* $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 record-existing when updated, does not exist when added-equivalent 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 Records
* $mongo->remove ("TTT", Array ("title" = "BBB"));
* Delete Only one record
* $mongo->remove ("TTT", Array ("title" = "BBB"), Array ("Justone" =>1));
* Get error message for MONGO operation
* $mongo->geterror ();
*/
Class Hmongodb {
Private $mongo; MongoDB Connection
Private $curr _db_name;
Private $curr _table_name;
Private $error;
Public Function getinstance ($mongo _server, $flag =array ())
{
Static $mongodb _arr;
if (Empty ($flag [' tag '])
{
$flag [' tag '] = ' default '; }
if (Isset ($flag [' Force ']) && $flag [' force '] = = true)
{
$mongo = new Hmongodb ($mongo _server);
if (Empty ($mongodb _arr[$flag [' tag ']])
{
$mongodb _arr[$flag [' tag '] = $mongo;
}
return $mongo;
}
else if (isset ($mongodb _arr[$flag [' tag ']) && is_resource ($mongodb _arr[$flag [' tag ']))
{
return $mongodb _arr[$flag [' tag '];
}
Else
{
$mongo = new Hmongodb ($mongo _server);
$mongodb _arr[$flag [' tag '] = $mongo;
return $mongo;
}
}
/**
* Constructor function
* Support Incoming multiple mongo_server (1. One problem when connecting to other server 2. Automatically distribute queries evenly to different servers)
*
Parameters
* $mongo _server: Array or String-array ("127.0.0.1:1111", "127.0.0.1:2222")-"127.0.0.1:1111"
* $connect: If the MONGO object is initialized with a connection, the default connection
* $auto _balance: Whether to do load balancing automatically, 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 _server[$key];
}
}
}
Else
{
$mongo _server_str = Implode (', ', $mongo _server);
}
}
Else
{
$mongo _server_str = $mongo _server;
}
try {
if ($mongo _server) {
$this->mongo = new MONGO ($mongo _server, Array (' Connect ' = $connect));
}else{
$this->mongo = new MONGO ();
}
}
catch (Mongoconnectionexception $e)
{
$this->error = $e->getmessage ();
return false;
}
}
/**
* Connect to MongoDB Server
*
* Parameters: None
*
* Return value:
* Success: TRUE
* Failed: false
*/
Public Function Connect ()
{
try {
$this->mongo->connect ();
return true;
}
catch (Mongoconnectionexception $e)
{
$this->error = $e->getmessage ();
return false;
}
}
/**
* Select DB
*
* Parameters: $dbname
*
* Return value: None
*/
Public Function Selectdb ($dbname)
{
$this->curr_db_name = $dbname;
}
/**
* CREATE INDEX: Returns if the index already exists.
*
Parameters
* $table _name: Table name
* $index: Index-array ("id" =>1)-establish ascending index in ID field
* $index _param: Other conditions-whether unique index, etc.
*
* Return value:
* Success: TRUE
* Failed: false
*/
Public Function Ensureindex ($table _name, $index, $index _param=array ())
{
$dbname = $this->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 Function Insert ($table _name, $record)
{
$dbname = $this->curr_db_name;
try {
$this->mongo-> $dbname, $table _name->insert ($record, Array (' safe ' =>true));
return true;
}
catch (Mongocursorexception $e)
{
$this->error = $e->getmessage ();
return false;
}
}
/**
* Number of records in the query table
*
Parameters
* $table _name: Table name
*
* Return Value: The number of records in the table
*/
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 conditions
* $newdata: New data record
* $options: Update selection-upsert/multiple
*
* Return value:
* Success: 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, Array (' $set ' + $newdata), $options);
return true;
}
catch (Mongocursorexception $e)
{
$this->error = $e->getmessage ();
return false;
}
}
/**
* Delete Records
*
Parameters
* $table _name: Table name
* $condition: Delete condition
* $options: Delete Select-justone
*
* Return value:
* Success: TRUE
* Failed: false
*/
Public function Remove ($table _name, $condition, $options =array ())
{
$dbname = $this->curr_db_name;
$options [' safe '] = 1;
try {
$table _name->remove, $this->mongo-> $dbname ($condition, $options);
return true;
}
catch (Mongocursorexception $e)
{
$this->error = $e->getmessage ();
return false;
} }
/**
* Find Records
*
Parameters
* $table _name: Table name
* $query _condition: Field Lookup criteria
* $result _condition: Query Results Restrictions-limit/sort, etc.
* $fields: Get field
*
* Return value:
* Success: Record set
* 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 ();
}
}
catch (Mongoconnectionexception $e)
{
$this->error = $e->getmessage ();
return false;
}
catch (Mongocursortimeoutexception $e)
{
$this->error = $e->getmessage ();
return false;
}
return $result;
}
/**
* Find a record
*
Parameters
* $table _name: Table name
* $condition: Find a condition
* $fields: Get field
*
* Return value:
* Success: A record
* Failed: false
*/
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
*
* Parameters: None
*
* Return Value: Current error message
*/
Public Function GetError ()
{
return $this->error;
}
/*** MongoDB Class * * Examples:
* $mongo = new Hmongodb ("127.0.0.1:11223");
* $mongo->selectdb ("test_db");
* Create an index
* $mongo->ensureindex ("test_table", Array ("id" =>1), Array (' unique ' =>true));
* Get Records of tables
* $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 record-existing when updated, does not exist when added-equivalent 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 Records
* $mongo->remove ("TTT", Array ("title" = "BBB"));
* Delete Only one record
* $mongo->remove ("TTT", Array ("title" = "BBB"), Array ("Justone" =>1));
* Get error message for MONGO operation
* $mongo->geterror ();
*/
}
MongoDB database Simple Class