A PHP Class Wrapper for MongoDB that provides an Active Record method with which to interact with a MongoDB system. Allows programmatic query creation and execution for CRUD operations.
Note: Every time you perform an operation ( even within the same PHP function ), you must re-define the Collection you're drawing from, as the query resets itself each time it runs. This is to avoid situations in which Collections are modified unintentionally due to not resetting the collection name after the query is complete.
- Edit Mongo.php to add your configuration options for the
DB_NAME
(the default DB to be used in queries) andDB_HOST
(your local MongoDB host location) - Add the Mongo.php file to your application with
require '{path_to_file}/Mongo.php';
- Define your Mongo instance with
$mongo = MongoAPI::getInstance();
You may then access the various methods with your $mongo
object, e.g. $mongo->get()
or $mongo->where()
Sets the DB select the collections from
$db_name
- (string) (required) - The name of the DB
$this->db( 'my_db_name' );
Sets Collection from which to draw records
$collection_name
- (string) (required) - The name of the collection
$this->db( 'my_collection_name' );
Defines conditions that must be met in the query. You may use where()
, and_where()
, and or_where()
to combine conditions.
Note: You may only use and_where()
OR or_where()
in a single query. You cannot combine the two at this time.
$key
- (string) (required) - The key to be matched against
$value
- (string) (required) - The value to match against the given key
$compare
- (string) (optional) - The operator to use for the comparison ('=', '!=', '>', '>=', '<', '<='). Default is '='
// Single where condition
$this->where( 'foo', 'bar' );
$rows = $this->get();
// Multiple where conditions (AND)
$this->where( 'foo', 'bar', '=' );
$this->and_where( 'blah', 'bleck', '!=' );
$rows = $this->get();
// Multiple where conditions (OR)
$this->where( 'foo', 'bar', '=' );
$this->or_where( 'blah', 'bleck', '!=' );
$rows = $this->get();
Returns a single row matching the given WHERE statements
No parameters (set WHERE conditions using $this->where()
$this->where( 'foo', 'bar', '=' );
$row = $this->getRow();
Returns an array of all rows matching the given WHERE statements
No parameters (set WHERE conditions using $this->where()
$this->where( 'foo', 'bar', '=' );
$rows = $this->get();
Inserts the given data into the chosen collection
$data
- (array) (required) - The data to insert into the collection
$data = array( 'foo' => 'bar', 'this' => 'that' );
$rows = $this->insert( $data );
Inserts an array of data into the Collection all at once.
$data_set
- (array) (required) - A multidimensional array of data to insert into the Collection
$data_set = array(
array( 'foo' => 'bar', 'this' => 'that' ),
array( 'foo' => 'baz', 'this' => 'nothing' )
);
$rows = $this->batchInsert( $data_set );
Updates records matching the WHERE conditions with the provided data array
$data
- (array) (required) - An array of key/value pairs to update in the matching records
$multiple
- (boolean) (optional) - If set to true, all matching records will be updated (defaults to false)
$upsert
- (boolean) (optional) - If set to true, a record will be added to the Collection if no matching records can be found
// Update the first matching record where foo == bar
$data = array( 'foo' => 'bar' );
$this->where( 'this' => 'that' );
$this->update( $data );
// Update all records where foo == bar
$data = array( 'foo' => 'bar' );
$this->where( 'this' => 'that' );
$this->update( $data, true );
// Insert a new record if no records match foo == bar
$data = array( 'foo' => 'bar' );
$this->where( 'this' => 'that' );
$this->update( $data, true, true );
Deletes records matching the WHERE condition
No parameters (set WHERE conditions using $this->where()
$this->where( 'foo', 'bar', '=' );
$rows = $this->delete();
Look in the /examples folder for sample files containing working code for each operation. (Be sure to set up your configuration options in /src/Mongo.php before running the sample files).
This wrapper supports a single-line notation format. The following operations will result in the same action:
$this->db( 'my_db_name' );
$this->collection( 'my_collection_name' );
$this->where( 'foo', 'bar', '!=' );
$this->delete();
$this->db( 'my_db_name' )->collection( 'my_collection_name')->where( 'foo', 'bar', '!=' )->delete();