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', ], ], ] ~~~
Since: 2.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Component
Datei anzeigen 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

Method Description
close ( ) Closes the currently active DB connection.
createCommand ( array $document = [], string | null $databaseName = null ) : Command Creates MongoDB command.
getCollection ( string | array $name, boolean $refresh = false ) : Collection Returns the MongoDB collection with the given name.
getDatabase ( string | null $name = null, boolean $refresh = false ) : Database Returns the MongoDB database with the given name.
getDefaultDatabaseName ( ) : string Returns default database name, if it is not set, attempts to determine it from [[dsn]] value.
getFileCollection ( string | array $prefix = 'fs', boolean $refresh = false ) : Collection Returns the MongoDB GridFS collection.
getIsActive ( ) : boolean Returns a value indicating whether the Mongo connection is established.
getLogBuilder ( ) : yii\mongodb\LogBuilder Returns log builder for this connection.
getQueryBuilder ( ) : QueryBuilder Returns the query builder for the this MongoDB connection.
open ( ) Establishes a Mongo connection.
registerFileStreamWrapper ( boolean $force = false ) : string Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
setDefaultDatabaseName ( string $name ) Sets default database name.
setLogBuilder ( array | string | yii\mongodb\LogBuilder $logBuilder ) Sets log builder used for this connection.
setQueryBuilder ( QueryBuilder | array | string | null $queryBuilder ) Sets the query builder for the this MongoDB connection.

Protected Methods

Method Description
initConnection ( ) Initializes the DB connection.
selectDatabase ( string $name ) : Database Selects the database with given name.

Method Details

close() public method

It does nothing if the connection is already closed.
public close ( )

createCommand() public method

Creates MongoDB command.
Since: 2.1
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.
public getDefaultDatabaseName ( ) : string
return string default database name

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.
public getIsActive ( ) : boolean
return boolean whether the Mongo connection is established

getLogBuilder() public method

Returns log builder for this connection.
Since: 2.1
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.
Since: 2.1
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.
protected initConnection ( )

open() public method

It does nothing if a MongoDB connection has already been established.
public open ( )

registerFileStreamWrapper() public method

Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
public registerFileStreamWrapper ( boolean $force = false ) : string
$force boolean whether to enforce registration even wrapper has been already registered.
return string registered stream protocol name.

selectDatabase() protected method

Selects the database with given name.
protected selectDatabase ( string $name ) : Database
$name string database name.
return Database database instance.

setDefaultDatabaseName() public method

Sets default database name.
public setDefaultDatabaseName ( string $name )
$name string default database name.

setLogBuilder() public method

Sets log builder used for this connection.
Since: 2.1
public setLogBuilder ( array | string | yii\mongodb\LogBuilder $logBuilder )
$logBuilder array | string | yii\mongodb\LogBuilder the log builder for this connection.

setQueryBuilder() public method

Sets the query builder for the this MongoDB connection.
Since: 2.1
public setQueryBuilder ( QueryBuilder | array | string | null $queryBuilder )
$queryBuilder QueryBuilder | array | string | null the query builder for 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.
See also: http://php.net/manual/en/mongodb-driver-manager.construct.php
public $driverOptions

$dsn public_oe property

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
public $dsn

$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.
Since: 2.1
public $enableLogging

$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.
Since: 2.1
public $enableProfiling

$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.
See also: yii\mongodb\file\StreamWrapper
Since: 2.1
public $fileStreamProtocol

$fileStreamWrapperClass public_oe property

name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol.
Since: 2.1
public $fileStreamWrapperClass

$manager public_oe property

MongoDB driver manager.
Since: 2.1
public $manager

$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 ]
See also: https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
public $options

$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.
See also: http://php.net/manual/en/mongodb-driver-cursor.settypemap.php
Since: 2.1
public $typeMap