class MongoDB (View source)

Instances of this class are used to interact with a database.

Constants

PROFILING_OFF

Profiling is off.

PROFILING_SLOW

Profiling is on for slow operations (>100 ms).

PROFILING_ON

Profiling is on for all operations.

Properties

int $w
int $wtimeout

Methods

__construct(MongoClient $conn, string $name)

(PECL mongo >= 0.9.0)
Creates a new database This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {Mongo::__get()} or {Mongo::selectDB()}.

string
__toString()

The name of this database

__get(string $name)

(PECL mongo >= 1.0.2)
Gets a collection

array
getCollectionNames(bool $includeSystemCollections = false)

(PECL mongo >= 1.3.0)

getGridFS(string $prefix = "fs")

(PECL mongo >= 0.9.0)
Fetches toolkit for dealing with files stored in this database

int
getProfilingLevel()

(PECL mongo >= 0.9.0)
Gets this database's profiling level

bool
getSlaveOkay()

(PECL mongo >= 1.1.0)
Get slaveOkay setting for this database

int
setProfilingLevel(int $level)

(PECL mongo >= 0.9.0)
Sets this database's profiling level

array
drop()

(PECL mongo >= 0.9.0)
Drops this database

array
repair(bool $preserve_cloned_files = false, bool $backup_original_files = false)

Repairs and compacts this database

selectCollection(string $name)

(PECL mongo >= 0.9.0)
Gets a collection

bool
setSlaveOkay(bool $ok = true)

(PECL mongo >= 1.1.0)
Change slaveOkay setting for this database

createCollection(string $name, array $options)

Creates a collection

array
dropCollection(MongoCollection|string $coll)

(PECL mongo >= 0.9.0)
Drops a collection

array
listCollections(bool $includeSystemCollections = false)

(PECL mongo >= 0.9.0)
Get a list of collections in this database

array
createDBRef(string $collection, mixed $document_or_id)

(PECL mongo >= 0.9.0)
Creates a database reference

array
getDBRef(array $ref)

(PECL mongo >= 0.9.0)
Fetches the document pointed to by a database reference

array
getWriteConcern()

(PECL mongo >= 1.5.0)
Get the write concern for this database

array
execute(MongoCode|string $code, array $args = [])

(PECL mongo >= 0.9.3)
Runs JavaScript code on the database server.

array
command(array $data, array $options)

Execute a database command

array
lastError()

(PECL mongo >= 0.9.5)
Check if there was an error on the most recent db operation performed

array
prevError()

(PECL mongo >= 0.9.5)
Checks for the last error thrown during a database operation

array
resetError()

(PECL mongo >= 0.9.5)
Clears any flagged errors on the database

bool
forceError()

(PECL mongo >= 0.9.5)
Creates a database error

array
authenticate(string $username, string $password)

(PECL mongo >= 1.0.1)
Log in to this database

array
getReadPreference()

(PECL mongo >= 1.3.0)
Get the read preference for this database

bool
setReadPreference(string $read_preference, array $tags)

(PECL mongo >= 1.3.0)
Set the read preference for this database

bool
setWriteConcern(mixed $w, int $wtimeout)

(PECL mongo >= 1.5.0)

Details

__construct(MongoClient $conn, string $name)

(PECL mongo >= 0.9.0)
Creates a new database This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {Mongo::__get()} or {Mongo::selectDB()}.

Parameters

MongoClient $conn

Database connection.

string $name

Database name.

Exceptions

Exception

string __toString()

The name of this database

Return Value

string

Returns this database's name.

MongoCollection __get(string $name)

(PECL mongo >= 1.0.2)
Gets a collection

Parameters

string $name

The name of the collection.

Return Value

MongoCollection

array getCollectionNames(bool $includeSystemCollections = false)

(PECL mongo >= 1.3.0)

Parameters

bool $includeSystemCollections

[optional] Include system collections.

Return Value

array

Returns the names of the all the collections in the database as an https://secure.php.net/manual/en/language.types.array.php array.

MongoGridFS getGridFS(string $prefix = "fs")

(PECL mongo >= 0.9.0)
Fetches toolkit for dealing with files stored in this database

Parameters

string $prefix

[optional] The prefix for the files and chunks collections.

Return Value

MongoGridFS

Returns a new gridfs object for this database.

int getProfilingLevel()

(PECL mongo >= 0.9.0)
Gets this database's profiling level

Return Value

int

Returns the profiling level.

bool getSlaveOkay()

(PECL mongo >= 1.1.0)
Get slaveOkay setting for this database

Return Value

bool

Returns the value of slaveOkay for this instance.

int setProfilingLevel(int $level)

(PECL mongo >= 0.9.0)
Sets this database's profiling level

Parameters

int $level

Profiling level.

Return Value

int

Returns the previous profiling level.

array drop()

(PECL mongo >= 0.9.0)
Drops this database

Return Value

array

Returns the database response.

array repair(bool $preserve_cloned_files = false, bool $backup_original_files = false)

Repairs and compacts this database

Parameters

bool $preserve_cloned_files

[optional]

If cloned files should be kept if the repair fails.

bool $backup_original_files

[optional]

If original files should be backed up.

Return Value

array

Returns db response.

MongoCollection selectCollection(string $name)

(PECL mongo >= 0.9.0)
Gets a collection

Parameters

string $name

The collection name.

Return Value

MongoCollection

Returns a new collection object.

Exceptions

Exception

bool setSlaveOkay(bool $ok = true)

(PECL mongo >= 1.1.0)
Change slaveOkay setting for this database

Parameters

bool $ok

[optional]

If reads should be sent to secondary members of a replica set for all possible queries using this https://secure.php.net/manual/en/class.mongodb.php MongoDB instance.

Return Value

bool

Returns the former value of slaveOkay for this instance.

MongoCollection createCollection(string $name, array $options)

Creates a collection

Parameters

string $name

The name of the collection.

array $options

[optional]

An array containing options for the collections. Each option is its own element in the options array, with the option name listed below being the key of the element. The supported options depend on the MongoDB server version. At the moment, the following options are supported:

capped

If the collection should be a fixed size.

size

If the collection is fixed size, its size in bytes.

max

If the collection is fixed size, the maximum number of elements to store in the collection.

autoIndexId

If capped is TRUE you can specify FALSE to disable the automatic index created on the _id field. Before MongoDB 2.2, the default value for autoIndexId was FALSE.

Return Value

MongoCollection

Returns a collection object representing the new collection.

array dropCollection(MongoCollection|string $coll)

(PECL mongo >= 0.9.0)
Drops a collection

Parameters

MongoCollection|string $coll

MongoCollection or name of collection to drop.

Return Value

array

Returns the database response.

See also

MongoCollection::drop

array listCollections(bool $includeSystemCollections = false)

(PECL mongo >= 0.9.0)
Get a list of collections in this database

Parameters

bool $includeSystemCollections

[optional]

Include system collections.

Return Value

array

Returns a list of MongoCollections.

array createDBRef(string $collection, mixed $document_or_id)

(PECL mongo >= 0.9.0)
Creates a database reference

Parameters

string $collection

The collection to which the database reference will point.

mixed $document_or_id

If an array or object is given, its _id field will be used as the reference ID. If a {\MongoId} or scalar is given, it will be used as the reference ID.

Return Value

array

Returns a database reference array.

If an array without an _id field was provided as the document_or_id parameter, NULL will be returned.

array getDBRef(array $ref)

(PECL mongo >= 0.9.0)
Fetches the document pointed to by a database reference

Parameters

array $ref

A database reference.

Return Value

array

Returns the document pointed to by the reference.

array getWriteConcern()

(PECL mongo >= 1.5.0)
Get the write concern for this database

Return Value

array

This function returns an array describing the write concern. The array contains the values w for an integer acknowledgement level or string mode, and wtimeout denoting the maximum number of milliseconds to wait for the server to satisfy the write concern.

array execute(MongoCode|string $code, array $args = [])

(PECL mongo >= 0.9.3)
Runs JavaScript code on the database server.

Parameters

MongoCode|string $code

Code to execute.

array $args

[optional] Arguments to be passed to code.

Return Value

array

Returns the result of the evaluation.

array command(array $data, array $options)

Execute a database command

Parameters

array $data

The query to send.

array $options

[optional]

This parameter is an associative array of the form array("optionname" => <boolean>, ...). Currently supported options are:

  • "timeout"

    Deprecated alias for "socketTimeoutMS".

Return Value

array

Returns database response. Every database response is always maximum one document, which means that the result of a database command can never exceed 16MB. The resulting document's structure depends on the command, but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents.

array lastError()

(PECL mongo >= 0.9.5)
Check if there was an error on the most recent db operation performed

Return Value

array

Returns the error, if there was one.

array prevError()

(PECL mongo >= 0.9.5)
Checks for the last error thrown during a database operation

Return Value

array

Returns the error and the number of operations ago it occurred.

array resetError()

(PECL mongo >= 0.9.5)
Clears any flagged errors on the database

Return Value

array

Returns the database response.

bool forceError()

(PECL mongo >= 0.9.5)
Creates a database error

Return Value

bool

Returns the database response.

array authenticate(string $username, string $password)

(PECL mongo >= 1.0.1)
Log in to this database

Parameters

string $username

The username.

string $password

The password (in plaintext).

Return Value

array

Returns database response. If the login was successful, it will return 1.

<?php
array( "ok"  =>  1 );
?>

If something went wrong, it will return

<?php
array( "ok"  =>  0 "errmsg"  =>  "auth fails" );
?>

("auth fails" could be another message, depending on database version and what went wrong)

array getReadPreference()

(PECL mongo >= 1.3.0)
Get the read preference for this database

Return Value

array

This function returns an array describing the read preference. The array contains the values type for the string read preference mode (corresponding to the MongoClient constants), and tagsets containing a list of all tag set criteria. If no tag sets were specified, tagsets will not be present in the array.

bool setReadPreference(string $read_preference, array $tags)

(PECL mongo >= 1.3.0)
Set the read preference for this database

Parameters

string $read_preference

The read preference mode: MongoClient::RP_PRIMARY, MongoClient::RP_PRIMARY_PREFERRED, MongoClient::RP_SECONDARY, MongoClient::RP_SECONDARY_PREFERRED, or MongoClient::RP_NEAREST.

array $tags

[optional]

An array of zero or more tag sets, where each tag set is itself an array of criteria used to match tags on replica set members.

Return Value

bool

Returns TRUE on success, or FALSE otherwise.

bool setWriteConcern(mixed $w, int $wtimeout)

(PECL mongo >= 1.5.0)

Parameters

mixed $w

The write concern. This may be an integer denoting the number of servers required to acknowledge the write, or a string mode (e.g. "majority").

int $wtimeout

[optional]

The maximum number of milliseconds to wait for the server to satisfy the write concern.

Return Value

bool

Returns TRUE on success, or FALSE otherwise.