Methods

The class constructor Automatically check if the Mongo PECL extension has been installed/enabled.

__construct(array $config) 

Generate the connection string and establish a connection to the MongoDB.

Parameters

$config

array

an array of config values

Exceptions

\Mongo_DbException

Ensure an index of the keys in a collection with optional parameters.

add_index(string $collection, array $keys, array $options) : \Fuel\Core\Mongo_Db

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).

usage $mongodb->add_index($collection, array('first_name' => 'ASC', 'last_name' => -1), array('unique' => TRUE));
fluent This method is part of a fluent interface and will return the same instance

Parameters

$collection

string

the collection name

$keys

array

an associative array of keys, array(field => direction)

$options

array

an associative array of options

Exceptions

\Mongo_DbException

Returns

Runs a MongoDB command (such as GeoNear).

command(array $query) : mixed

See the MongoDB documentation for more usage scenarios: http://dochub.mongodb.org/core/commands

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

Parameters

$query

array

a query array

Exceptions

\Mongo_DbException

Returns

mixed

Count the documents based upon the passed parameters

count(string $collection, boolean $foundonly) : mixed

usage $mongodb->count('foo');

Parameters

$collection

string

the collection name

$foundonly

boolean

send cursor limit and skip information to the count function, if applicable.

Exceptions

\Mongo_DbException

Returns

mixed

Delete a document from the passed collection based upon certain criteria

delete(string $collection) : bool

usage $mongodb->delete('foo');

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Returns

bool

Delete all documents from the passed collection based upon certain criteria.

delete_all(string $collection) : bool

usage $mongodb->delete_all('foo');

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Returns

bool

Drop a Mongo collection

drop_collection(string $db, string $col) : bool

usage $mongodb->drop_collection('foo', 'bar');

Parameters

$db

string

the database name

$col

string

the collection name

Exceptions

\Mongo_DbException

Returns

bool

Drop a Mongo database

drop_db(string $database) : bool

usage $mongodb->drop_db("foobar");

Parameters

$database

string

the database name

Exceptions

\Mongo_DbException

Returns

bool

Dump database or collection

dump(mixed $collection_name, string $path) : bool

usage $mongodb->dump();
usage $mongodb->dump(test, APPPATH . "tmp");
usage $mongodb->dump(["test", "test2"], APPPATH . "tmp");

Parameters

$collection_name

mixed

The collection name

$path

string

Path to write the dump do

Returns

bool

Get the documents based upon the passed parameters

get(string $collection) : array

usage $mongodb->get('foo', array('bar' => 'something'));

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Returns

array

Returns a collection object so you can perform advanced queries, upserts, pushes and addtosets

get_collection(string $collection) 

usage $collection_name = $mongodb->get_collection('collection_name');

Parameters

$collection

string

the collection name

Get the document cursor from mongodb based upon the passed parameters

get_cursor(string $collection) 

usage $mongodb->get_cursor('foo', array('bar' => 'something'));

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Get one document based upon the passed parameters

get_one(string $collection) : mixed

usage $mongodb->get_one('foo');

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Returns

mixed

Get the documents based upon the passed parameters

get_where(string $collection, array $where, integer $limit) : array

usage $mongodb->get_where('foo', array('bar' => 'something'));

Parameters

$collection

string

the collection name

$where

array

an array of conditions, array(field => value)

$limit

integer

the max amount of documents to fetch

Returns

array

-------------------------------------------------------------------------------- INSERT --------------------------------------------------------------------------------

insert(string $collection, array $insert) : bool

Insert a new document into the passed collection

usage $mongodb->insert('foo', $data = array());

Parameters

$collection

string

the collection name

$insert

array

an array of values to insert, array(field => value)

Exceptions

\Mongo_DbException

Returns

bool

Acts as a Multiton.

instance(string $name) : \Fuel\Core\Mongo_Db

Will return the requested instance, or will create a new one if it does not exist.

Parameters

$name

string

The instance name

Exceptions

\Mongo_DbException

Returns

Limit the result set to $x number of documents

limit(integer $x) : \Fuel\Core\Mongo_Db

usage $mongodb->limit($x);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$x

integer

the max amount of documents to fetch

Returns

Returns all collection objects

list_collections(bool $system_collections) 

usage $collections = $mongodb->list_collections();

Parameters

$system_collections

bool

whether or not to include system collections

Lists all indexes in a collection.

list_indexes(string $collection) : mixed

usage $mongodb->list_indexes($collection);

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Returns

mixed

-------------------------------------------------------------------------------- OFFSET DOCUMENTS --------------------------------------------------------------------------------

offset(integer $x) : \Fuel\Core\Mongo_Db

Offset the result set to skip $x number of documents

usage $mongodb->offset($x);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$x

integer

the number of documents to skip

Returns

Get the documents where the value of a $field may be something else

or_where(array $wheres) : \Fuel\Core\Mongo_Db

usage $mongodb->or_where(array( array('foo'=>'bar', 'bar'=>'foo' ))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$wheres

array

an associative array with conditions, array(field => value)

Returns

Sort the documents based on the parameters passed.

order_by(array $fields) : \Fuel\Core\Mongo_Db

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).

usage $mongodb->where_between('foo', 20, 30);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$fields

array

an associative array, array(field => direction)

Returns

Remove all indexes from a collection.

remove_all_indexes(string $collection) : \Fuel\Core\Mongo_Db

usage $mongodb->remove_all_index($collection);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$collection

string

the collection name

Exceptions

\Mongo_DbException

Returns

Remove an index of the keys in a collection.

remove_index(string $collection, array $keys) : \Fuel\Core\Mongo_Db

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).

usage $mongodb->remove_index($collection, array('first_name' => 'ASC', 'last_name' => -1));
fluent This method is part of a fluent interface and will return the same instance

Parameters

$collection

string

the collection name

$keys

array

an associative array of keys, array(field => direction)

Exceptions

\Mongo_DbException

Returns

Determine which fields to include OR which to exclude during the query process.

select(array $includes, array $excludes) : \Fuel\Core\Mongo_Db

Currently, including and excluding at the same time is not available, so the $includes array will take precedence over the $excludes array. If you want to only choose fields to exclude, leave $includes an empty array().

usage $mongodb->select(array('foo', 'bar'))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$includes

array

which fields to include

$excludes

array

which fields to exclude

Returns

Updates a single document

update(string $collection, array $data, array $options, bool $literal) : bool

usage $mongodb->update('foo', $data = array());

Parameters

$collection

string

the collection name

$data

array

an associative array of values, array(field => value)

$options

array

an associative array of options

$literal

bool

Exceptions

\Mongo_DbException

Returns

bool

Updates a collection of documents

update_all(string $collection, array $data, bool $literal) : bool

usage $mongodb->update_all('foo', $data = array());

Parameters

$collection

string

the collection name

$data

array

an associative array of values, array(field => value)

$literal

bool

Exceptions

\Mongo_DbException

Returns

bool

Get the documents based on these search parameters.

where(array $wheres) : \Fuel\Core\Mongo_Db

The $wheres array should be an associative array with the field as the key and the value as the search criteria.

usage $mongodb->where(array('foo' => 'bar'))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$wheres

array

an associative array with conditions, array(field => value)

Returns

Get the documents where the value of a $field is between $x and $y

where_between(string $field, mixed $x, mixed $y) : \Fuel\Core\Mongo_Db

usage $mongodb->where_between('foo', 20, 30);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$x

mixed

the value to compare to

$y

mixed

the high value to compare to

Returns

Get the documents where the value of a $field is between but not equal to $x and $y

where_between_ne(string $field, mixed $x, mixed $y) : \Fuel\Core\Mongo_Db

usage $mongodb->where_between_ne('foo', 20, 30);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$x

mixed

the low value to compare to

$y

mixed

the high value to compare to

Returns

Get the documents where the value of a $field is greater than $x

where_gt(string $field, mixed $x) : \Fuel\Core\Mongo_Db

usage $mongodb->where_gt('foo', 20);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$x

mixed

the value to compare to

Returns

Get the documents where the value of a $field is greater than or equal to $x

where_gte(string $field, mixed $x) : \Fuel\Core\Mongo_Db

usage $mongodb->where_gte('foo', 20);

Parameters

$field

string

the field name

$x

mixed

the value to compare to

Returns

Get the documents where the value of a $field is in a given $in array().

where_in(string $field, array $in) : \Fuel\Core\Mongo_Db

usage $mongodb->where_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$in

array

an array of values to compare to

Returns

Get the documents where the value of a $field is in all of a given $in array().

where_in_all(string $field, array $in) : \Fuel\Core\Mongo_Db

usage $mongodb->where_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$in

array

an array of values to compare to

Returns

Get the documents where the value of a $field is less than $x

where_lt(string $field, mixed $x) : \Fuel\Core\Mongo_Db

usage $mongodb->where_lt('foo', 20);

Parameters

$field

string

the field name

$x

mixed

the value to compare to

Returns

Get the documents where the value of a $field is less than or equal to $x

where_lte(string $field, mixed $x) : \Fuel\Core\Mongo_Db

usage $mongodb->where_lte('foo', 20);
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$x

mixed

the value to compare to

Returns

Get the documents where the value of a $field is not equal to $x

where_ne(string $field, mixed $x) : \Fuel\Core\Mongo_Db

usage $mongodb->where_not_equal('foo', 1)->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$x

mixed

the value to compare to

Returns

Get the documents nearest to an array of coordinates (your collection must have a geospatial index)

where_near(string $field, array $co) : \Fuel\Core\Mongo_Db

usage $mongodb->where_near('foo', array('50','50'))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$co

array

array of 2 coordinates

Returns

Get the documents where the value of a $field is not in a given $in array().

where_not_in(string $field, array $in) : \Fuel\Core\Mongo_Db

usage $mongodb->where_not_in('foo', array('bar', 'zoo', 'blah'))->get('foobar');
fluent This method is part of a fluent interface and will return the same instance

Parameters

$field

string

the field name

$in

array

an array of values to compare to

Returns

Resets the class variables to default settings

_clear() 

Prepares parameters for insertion in $wheres array().

_where_init(string $param) 

Parameters

$param

string

the field name

Collect and write dump

_write_dump(string $path, \Fuel\Core\MongoCollection $mongo_collection) 

Parameters

$path

string

$mongo_collection

\Fuel\Core\MongoCollection

 Properties

 

Holds all the where options.

$wheres : array

 

Holds the current Mongo connection object

$connection : \Fuel\Core\Mongo

 

Holds the current DB reference on the connection object

$db : Object

 

All the Mongo_Db instances

$instances : array

 

Holds the limit of the number of results to return

$limit : int

 

The offset to start from.

$offset : int

 

Whether to use a persistent connection

$persist : bool

 

Whether to use the profiler

$profiling : bool

 

Holds all the select options

$selects : array

 

Holds the sorting options

$sorts : array