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
パラメータ
パラメータ 規定値 説明
$collection string
'default'
The name of the config group to use.
返り値 A new Mongo_Db instance
例外 Mongo_DbException, when no config if found for the given instance name.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection name.
返り値 A stdClass object containing the result arrays.
// Get an instance
$mongodb = \Mongo_Db::instance();

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

get_one($collection = "")

The get_one method returns one result from a collection.

Static No
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection name.
返り値 A stdClass object containing the single result array.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection name.
$where array
array()
An array of where statements.
返り値 A stdClass object containing the result arrays.
// 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
パラメータ
パラメータ 規定値 説明
$includes array
array()
Array of fields to include
$where array
array()
Array of fields to exclude
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$wheres array
array()
An array of associative array with the field as the key and the value as the search criteria.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$wheres array
array()
An array of associative array with the field as the key and the value as the search criteria.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$in array
array()
An array of possible values.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$in array
array()
An array of possible values.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$in array
array()
An array of possible values.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$x mixed required The value to compare it to.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$x mixed required The value to compare it to.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$x mixed required The value to compare it to.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$x mixed required The value to compare it to.
返り値 The current Mongo_Db instance.
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_lt('age', 120);
$users = $mongodb->get('users');

where_between($field = "", $x)

The where_between method sets the 'where between' statement.

Static No
パラメータ
パラメータ 規定値 説明
$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.
返り値 The current Mongo_Db instance.
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_between('age', 10, 20);
$users = $mongodb->get('users');

where_between_ne($field = "", $x)

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

Static No
パラメータ
パラメータ 規定値 説明
$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.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$field string
''
The field to query.
$x mixed required The low value to compare it to.
返り値 The current Mongo_Db instance.
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_ne('name', 'Allerd');
$users = $mongodb->get('users');

like($field = '', $value = '', $flags = 'i', $enable_start_wildcard = true, $enable_end_wildcard = true)

The like method a sets a 'like' statement.

Static No
パラメータ
パラメータ 規定値 説明
$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
$enable_start_wildcard bool
false
When set to true the beginning of the string must match.
$enable_end_wildcard bool
false
When set to true the ending of the string must match.
返り値 The current Mongo_Db instance.
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->like('name', 'fran', 'im', true);
$users = $mongodb->get('users');

order_by($fields = array())

The order_by method orders the result.

Static No
パラメータ
パラメータ 規定値 説明
$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).
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$x number
99999
The number of items to fetch.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$x number
0
The number of items to skip.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
$foundonly boolean
false
if true, it will take the current limit and skip selections into account.
返り値 The number of rows.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
$insert array
array()
The values to insert
返り値 The insert id, or false on failure.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
$data array
array()
The new values.
$options array
array()
Supply optional extra options (?) to the update command.
返り値 true on success
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
$data array
array()
The new values.
返り値 true on success
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
返り値 true on success
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
返り値 true on success
// 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
パラメータ
パラメータ 規定値 説明
$query array
array()
An array of command parameters.
返り値 The command result
// 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
パラメータ
パラメータ 規定値 説明
$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.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
$keys array
array()
An associative array with fields (and sorting directions).
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
返り値 The current Mongo_Db instance.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
返り値 An array of index names for this collection.
// 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
パラメータ
パラメータ 規定値 説明
$collection string
''
The collection (table) to use.
返り値 A MongoCollection object from the core MongoDB PHP Driver.
// 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.