-
Notifications
You must be signed in to change notification settings - Fork 50
Mongo_Database
This class wraps the functionality of Mongo (connection) and MongoDB (database object) into one class.
When used with Kohana it can be instantiated simply by:
$db = Mongo_Database::instance();
The above will assume the ‘default’ configuration from the APPPATH/config/mongo.php file.
Alternatively (required for use outside of Kohana) it may be instantiated with the name and configuration specified as arguments:
$db = Mongo_Database::instance('test', array(
'database' => 'test'
));
If using Kohana, profiling can be enabled/disabled via the configuration or on demand by setting the profiling
property.
The Mongo_Collection class will gain access to the server by calling the instance method with a configuration name, so if not using Kohana or the configuration name is not present in the config file then the instance should be created before using any classes that extend Mongo_Collection or Mongo_Document.
Mongo_Database can proxy all methods of MongoDB to the database instance using the __call magic method which adds profiling, as well as select collections using the __get magic method. Only operations that require round trips to the database will be profiled. However, the intended usage is to not directly select collections from the database, but rather to instantiate Mongo_Collection and Mongo_Document as needed.
Connecting and instantiating the wrapped objects is handled lazily, so unless you need to perform advanced operations such as Mongo_Database::instance()->execute(array(...))
or similar, you won’t be using this class much directly. Also don’t be afraid to instantiate it early in your application code since that alone is a very inexpensive process.
This class was originally adapted from MangoDB but has since been completely rewritten.