PHP Class yii\mongodb\Connection
Connection works together with
Database and
Collection to provide data access
to the Mongo database. They are wrappers of the [[MongoDB PHP extension]](http://us1.php.net/manual/en/book.mongo.php).
To establish a DB connection, set [[dsn]] and then call
Connection::open to be true.
The following example shows how to create a Connection instance and establish
the DB connection:
~~~
$connection = new \yii\mongodb\Connection([
'dsn' => $dsn,
]);
$connection->open();
~~~
After the Mongo connection is established, one can access Mongo databases and collections:
~~~
$database = $connection->getDatabase('my_mongo_db');
$collection = $database->getCollection('customer');
$collection->insert(['name' => 'John Smith', 'status' => 1]);
~~~
You can work with several different databases at the same server using this class.
However, while it is unlikely your application will actually need it, the Connection class
provides ability to use [[defaultDatabaseName]] as well as a shortcut method
Connection::getCollection
to retrieve a particular collection instance:
~~~
get collection 'customer' from default database:
$collection = $connection->getCollection('customer');
get collection 'customer' from database 'mydatabase':
$collection = $connection->getCollection(['mydatabase', 'customer']);
~~~
Connection is often used as an application component and configured in the application
configuration like the following:
~~~
[
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',
],
],
]
~~~
Exibir arquivo
Open project: yiisoft/yii2-mongodb
Class Usage Examples
Public Properties
Property |
Type |
Description |
|
$driverOptions |
|
options for the MongoDB driver.
Any driver-specific options not included in MongoDB connection string specification. |
|
$dsn |
|
host:port
Correct syntax is:
mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname]
For example:
mongodb://localhost:27017
mongodb://developer:password@localhost:27017
mongodb://developer:password@localhost:27017/mydatabase |
|
$enableLogging |
|
whether to log command and query executions.
When enabled this option may reduce performance. MongoDB commands may contain large data,
consuming both CPU and memory.
It makes sense to disable this option in the production environment. |
|
$enableProfiling |
|
whether to enable profiling the commands and queries being executed.
This option will have no effect in case [[enableLogging]] is disabled. |
|
$fileStreamProtocol |
|
name of the protocol, which should be used for the GridFS stream wrapper.
Only alphanumeric values are allowed: do not use any URL special characters, such as '/', '&', ':' etc. |
|
$fileStreamWrapperClass |
|
name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol. |
|
$manager |
|
MongoDB driver manager. |
|
$options |
|
connection options.
For example:
php
[
'socketTimeoutMS' => 1000, // how long a send or receive on a socket can take before timing out
'ssl' => true // initiate the connection with TLS/SSL
]
|
|
$typeMap |
|
type map to use for BSON unserialization.
Note: default type map will be automatically merged into this field, possibly overriding user-defined values. |
|
Public Methods
Protected Methods
Method Details
It does nothing if the connection is already closed.
createCommand()
public method
public createCommand ( array $document = [], string | null $databaseName = null ) : Command |
$document |
array |
command document contents. |
$databaseName |
string | null |
database name, if not set [[defaultDatabaseName]] will be used. |
return |
Command |
command instance. |
getCollection()
public method
Returns the MongoDB collection with the given name.
public getCollection ( string | array $name, boolean $refresh = false ) : Collection |
$name |
string | array |
collection name. If string considered as the name of the collection
inside the default database. If array - first element considered as the name of the database,
second - as name of collection inside that database |
$refresh |
boolean |
whether to reload the collection instance even if it is found in the cache. |
return |
Collection |
Mongo collection instance. |
getDatabase()
public method
Returns the MongoDB database with the given name.
public getDatabase ( string | null $name = null, boolean $refresh = false ) : Database |
$name |
string | null |
database name, if null default one will be used. |
$refresh |
boolean |
whether to reestablish the database connection even, if it is found in the cache. |
return |
Database |
database instance. |
getDefaultDatabaseName()
public method
Returns default database name, if it is not set,
attempts to determine it from [[dsn]] value.
getFileCollection()
public method
Returns the MongoDB GridFS collection.
public getFileCollection ( string | array $prefix = 'fs', boolean $refresh = false ) : Collection |
$prefix |
string | array |
collection prefix. If string considered as the prefix of the GridFS
collection inside the default database. If array - first element considered as the name of the database,
second - as prefix of the GridFS collection inside that database, if no second element present
default "fs" prefix will be used. |
$refresh |
boolean |
whether to reload the collection instance even if it is found in the cache. |
return |
yii\mongodb\file\Collection |
Mongo GridFS collection instance. |
getIsActive()
public method
Returns a value indicating whether the Mongo connection is established.
getLogBuilder()
public method
Returns log builder for this connection.
public getLogBuilder ( ) : yii\mongodb\LogBuilder |
return |
yii\mongodb\LogBuilder |
the log builder for this connection. |
getQueryBuilder()
public method
Returns the query builder for the this MongoDB connection.
public getQueryBuilder ( ) : QueryBuilder |
return |
QueryBuilder |
the query builder for the this MongoDB connection. |
initConnection()
protected method
This method is invoked right after the DB connection is established.
The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
It does nothing if a MongoDB connection has already been established.
registerFileStreamWrapper()
public method
Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
selectDatabase()
protected method
Selects the database with given name.
setDefaultDatabaseName()
public method
Sets default database name.
setLogBuilder()
public method
Sets log builder used for this connection.
setQueryBuilder()
public method
Sets the query builder for the this MongoDB connection.
Property Details
$driverOptions public_oe property
options for the MongoDB driver.
Any driver-specific options not included in MongoDB connection string specification.
host:port
Correct syntax is:
mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname]
For example:
mongodb://localhost:27017
mongodb://developer:password@localhost:27017
mongodb://developer:password@localhost:27017/mydatabase
$enableLogging public_oe property
whether to log command and query executions.
When enabled this option may reduce performance. MongoDB commands may contain large data,
consuming both CPU and memory.
It makes sense to disable this option in the production environment.
$enableProfiling public_oe property
whether to enable profiling the commands and queries being executed.
This option will have no effect in case [[enableLogging]] is disabled.
$fileStreamProtocol public_oe property
name of the protocol, which should be used for the GridFS stream wrapper.
Only alphanumeric values are allowed: do not use any URL special characters, such as '/', '&', ':' etc.
public $fileStreamProtocol |
$fileStreamWrapperClass public_oe property
name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol.
public $fileStreamWrapperClass |
$manager public_oe property
$options public_oe property
connection options.
For example:
php
[
'socketTimeoutMS' => 1000, // how long a send or receive on a socket can take before timing out
'ssl' => true // initiate the connection with TLS/SSL
]
$typeMap public_oe property
type map to use for BSON unserialization.
Note: default type map will be automatically merged into this field, possibly overriding user-defined values.