Mongo_Db Class Methods

The Mongo_Db class allows you to interact with MongoDB databases. While in some cases it's similar to interacting with databases like MySQL there are some distinct differences.

All sorting options (order by and indexes) use -1, false, 'desc' or 'DESC' as DESC. Any other value will be ASC.

instance($name = 'default')

The instance method returns a new Mongo_Db instance. Use the $name parameter to specify which configuration and database to use.

By default $name is 'default' which requires you to have a default configuration in you db.php config file. Read more about Mongo_Db configuration.

Static Yes
Parameters
Param Type Default Description
$collection string
'default'
The name of the config group to use.
Returns A new Mongo_Db instance
Throws Mongo_DbException, when no config if found for the given instance name.
Example
// Get the default group
$mongodb = \Mongo_Db::instance();

// Get a custom group
$mongodb = \Mongo_Db::instance('my_group');

get($collection = "")

The get method returns the result from a collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection name.
Returns A stdClass object containing the result arrays.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$result = $mongodb->get('users');
foreach($result as $user)
{
	// Do something with $user
}

get_cursor($collection = "")

The get_cursor method returns the mongodb cursor from the specified collection. The advantage it provides over the normal get() method is when processing large datasets as documents are only loaded into memory one at a time as they are accessed instead of all at once. Very useful for long running processes.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection name.
Returns A MongoCursor object.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$result = $mongodb->get_cursor('documents');
foreach($result as $document)
{
	// Do something with $document
}

get_one($collection = "")

The get_one method returns one result from a collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection name.
Returns A stdClass object containing the single result array.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$user = $mongodb->get_one('users');

get_where($collection = "", $where = array(), $limit = 99999)

The get_where method returns the result from a collection. In addition to that you can supply an array for conditioning the result and provide a limit.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection name.
$where array
array()
An array of where statements.
$limit number
99999
The number of items to fetch.
Returns A stdClass object containing the result arrays.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Get the first 10 result where 'active' is "yes"
$result = $mongodb->get_where('users', array(
	'active' => 'yes',
), 10);

foreach($result as $user)
{
	// Do something with $user
}

select($includes = array(), $excludes = array())

The select method sets which fields to include or which to exclude during the query process.

In order to use the exclude array the includes array must be empty.

Static No
Parameters
Param Type Default Description
$includes array
array()
Array of fields to include
$excludes array
array()
Array of fields to exclude
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
// Just select these fields
$mongodb->select(array(
	'name',
	'surname',
	'email',
));
$users = $mongodb->get('users');

// Or use the exclude array

$mongodb = \Mongo_Db::instance();
// Just select these fields
$mongodb->select(array(), array(
	'not_this',
	'or_this',
));
$users = $mongodb->get('users');

where($wheres = array())

The where method sets the 'where is equal to' statement.

Static No
Parameters
Param Type Default Description
$wheres array
array()
An array of associative array with the field as the key and the value as the search criteria.
Returns The current Mongo_Db instance.
Example
// Get an instance.
$mongodb = \Mongo_Db::instance();

$mongodb->where(array(
	'name' => 'John',
	'surname' => 'Doe',
));

$users = $mongodb->get('users');

or_where($wheres = array())

The or_where method sets the 'or where is equal to' statement.

Static No
Parameters
Param Type Default Description
$wheres array
array()
An array of associative array with the field as the key and the value as the search criteria.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where(array(
	'name' => 'John',
	'surname' => 'Doe',
))->or_where(array(
	'name' => 'Jim',
));
$users = $mongodb->get('users');

where_in($field = "", $in = array())

The where_in method sets the 'where in' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$in array
array()
An array of possible values.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_in('name', array(
	'John', 'James', 'Frank', 'Dave',
));
$users = $mongodb->get('users');

where_in_all($field = "", $in = array())

The where_in_all method sets the 'all where in' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$in array
array()
An array of possible values.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_in_all('name', array(
	'John', 'James', 'Frank', 'Dave',
));
$users = $mongodb->get('users');

where_not_in($field = "", $in = array())

The where_not_in method sets the 'where not in' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$in array
array()
An array of possible values.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_not_in('name', array(
	'John', 'James', 'Frank', 'Dave',
));
$users = $mongodb->get('users');

where_gt($field = "", $x)

The where_gt method sets the 'where greater than' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_gt('age', 12);
$users = $mongodb->get('users');

where_gte($field = "", $x)

The where_gte method sets the 'where greater than or equal to' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_gte('age', 18);
$users = $mongodb->get('users');

where_lt($field = "", $x)

The where_lt method sets the 'where lower than' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_lt('age', 120);
$users = $mongodb->get('users');

where_lte($field = "", $x)

The where_lte method sets the 'where lower than or equal to' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_lt('age', 120);
$users = $mongodb->get('users');

where_between($field = "", $x, $y)

The where_between method sets the 'where between' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The low value to compare it to.
$y mixed required The high value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_between('age', 10, 20);
$users = $mongodb->get('users');

where_between_ne($field = "", $x, $y)

The where_between_ne method sets the 'where between and not equal to' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The low value to compare it to.
$y mixed required The high value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_between_ne('age', 10, 20);
$users = $mongodb->get('users');

where_ne($field = "", $x)

The where_ne method sets the 'where not equal to' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$x mixed required The low value to compare it to.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_ne('name', 'Allerd');
$users = $mongodb->get('users');

like($field = '', $value = '', $flags = 'i', $disable_start_wildcard = false, $disable_end_wildcard = false)

The like method a sets a 'like' statement.

Static No
Parameters
Param Type Default Description
$field string
''
The field to query.
$value mixed
''
The value to compare it to.
$flags string
'i'
Allows you to set typical regular expression flags:
  • i = case insensitive
  • m = multiline
  • x = can contain comments
  • l = locale
  • s = dotall, "." matches everything, including newlines
  • u = match unicode
$disable_start_wildcard bool
false
When set to false the beginning of the string must match.
$disable_end_wildcard bool
false
When set to false the ending of the string must match.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// look for users with a name starting with 'fran'...
$mongodb->like('name', 'fran', 'im', false, true);
$users = $mongodb->get('users');

order_by($fields = array())

The order_by method orders the result.

Static No
Parameters
Param Type Default Description
$fields array
array()
To set values to descending order, you must pass values of either -1, FALSE, 'desc', or 'DESC', else they will be set to 1 (ASC).
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->order_by(array(
	'age' => 'asc',
	'name' => 'desc'

));
$users = $mongodb->get('users');

limit($x = 99999)

The limit method limits the number of items fetched.

Static No
Parameters
Param Type Default Description
$x number
99999
The number of items to fetch.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
// Get the 10 oldest
$mongodb->order_by(array(
	'age' => 'desc',
))->limit(10);
$users = $mongodb->get('users');

offset($x = 0)

The offset method sets the number of items skip.

Static No
Parameters
Param Type Default Description
$x number
0
The number of items to skip.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();
// Get row 10 to 15
$mongodb->order_by(array(
	'age' => 'desc',
))->limit(5)->offset(10);
$users = $mongodb->get('users');

count($collection = '', $foundonly = false)

The count method counts the result.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
$foundonly boolean
false
if true, it will take the current limit and skip selections into account.
Returns The number of rows.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Get the amount of users.
$num_users = $mongodb->count('users');

insert($collection = '', $insert = array())

The insert method inserts a row into the given collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
$insert array
array()
The values to insert
Returns The insert id, or false on failure.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Insert a new user
$insert_id = $mongodb->insert('users', array(
	'name' => 'John',
	'surname' => 'Doe',
	'email' => 'dont.em@ilme.com',
));

update($collection = "", $data = array(), $options = array())

The update method updates a given collection.

If you do not set the "multiple" option to true only the first encountered record will be updated.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
$data array
array()
The new values.
$options array
array()
Supply optional extra options (?) to the update command.
Returns true on success
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Update a user
$bool = $mongodb->where(array('id' => $an_id))->update('users', array(
	'name' => 'John',
	'surname' => 'Doe',
	'email' => 'john@doe.com',
));

update_all($collection = "", $data = array())

The update_all method updates all records in a given collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
$data array
array()
The new values.
Returns true on success
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Update Spammer's surname.
$bool = $mongodb->where('name', 'Spammer')->update_all('users', array(
	'surname' => 'Don\'t believe us.',
));

delete($collection = "")

The delete method deletes the first record in a given collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
Returns true on success
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Delete a user named Spammer
$bool = $mongodb->where(array('name' => 'Spammer'))->delete('users');

delete_all($collection = "")

The delete_all method deletes all records in a given collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
Returns true on success
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

// Delete all users named Spammer
$bool = $mongodb->where(array('name' => 'Spammer'))->delete_all('users');

command($query = array())

The command method runs a MongoDB command. Find out more about commands at the MongoDB docs.

Static No
Parameters
Param Type Default Description
$query array
array()
An array of command parameters.
Returns The command result
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$mongodb->command(array(
	'geoNear' => 'buildings',
	'near' => array(53.228482, -0.547847),
	'num' => 10,
	'nearSphere' => true
));

add_index($collection = "", $keys = array(), $options = array())

The add_index method adds an index of the keys in a collection with optional parameters. Read more about the options at php.net.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
$keys array
array()
An associative array with fields and sorting directions.
$options array
array()
An array with optional settings.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$mongodb->add_index('my_collection', array(
	'first_name' => 'ASC',
	'last_name' => -1)
, array(
	'unique' => true
));

remove_index($collection = "", $keys = array())

The remove_index method removes one or more indexes from a collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
$keys array
array()
An associative array with fields (and sorting directions).
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$mongodb->remove_index('my_collection', array(
	'first_name' => 'ASC',
	'last_name' => -1)
));

// or

$mongodb->remove_index('first_name');

remove_all_indexes($collection = "")

The remove_all_indexes method removes all indexes from a collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
Returns The current Mongo_Db instance.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$mongodb->remove_all_indexes('my_collection');

list_indexes($collection = "")

The list_indexes method returns an array of index names for this collection.

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
Returns An array of index names for this collection.
Example
// Get an instance
$mongodb = \Mongo_Db::instance();

$indexes = $mongodb->list_indexes('my_collection');

get_collection($collection = "")

The get_collection method returns a MongoCollection object from the core MongoDB PHP Driver. (?)

Static No
Parameters
Param Type Default Description
$collection string
''
The collection (table) to use.
Returns A MongoCollection object from the core MongoDB PHP Driver.
Example
// Get a collection
$mongodb       = \Mongo_Db::instance();
$my_collection = $mongodb->get_collection('my_collection');
$my_collection->update(array('_id' => $id), array('$push' => array('array.to.add.to' => $data)));

This was put in place due to the inherent lack of support for update modifier operation.

list_collections($system_collections = false)

The list_collections method returns an array of MongoCollection objects of all collections present in the database. Optionally you can specify that you want all system collections returned too.

Static No
Parameters
Param Type Default Description
system_collections bool
false
Whether or not to include the system collections.
Returns An array of MongoCollection objects.
Example
// Get a mongo instance
$mongodb     = \Mongo_Db::instance();
$collections = $mongodb->list_collections();

dump($collection_name = null, $path = null)

The dump method allows you to dump one or more collections to a file, in json format.

Static No
Parameters
Param Type Default Description
collection_name mixed
null
A single string to specify a collection, an array of strings to specify multiple collections, or null for all collections.
path string
null
Path to write the dump to. If not given, the dump will be made to APPPATH."tmp/mongo-YYYYMMDD".
Returns Bool, true.
Throws InvalidPathException, if the file can not be written to the path given
FileAccessException, if there is no permission to write the file
Example
// Get a mongo instance
$mongodb = \Mongo_Db::instance();

// dump all collections to APPPATH.'tmp'
$mongodb->dump();