PHP 클래스 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',
],
],
]
~~~
파일 보기
프로젝트 열기: yiisoft/yii2-mongodb
1 사용 예제들
공개 프로퍼티들
프로퍼티 |
타입 |
설명 |
|
$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. |
|
공개 메소드들
보호된 메소드들
메소드 상세
It does nothing if the connection is already closed.
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. |
리턴 |
Command |
command instance. |
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. |
리턴 |
Collection |
Mongo collection instance. |
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. |
리턴 |
Database |
database instance. |
getDefaultDatabaseName()
공개 메소드
Returns default database name, if it is not set,
attempts to determine it from [[dsn]] value.
getFileCollection()
공개 메소드
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. |
리턴 |
yii\mongodb\file\Collection |
Mongo GridFS collection instance. |
Returns a value indicating whether the Mongo connection is established.
Returns log builder for this connection.
public getLogBuilder ( ) : yii\mongodb\LogBuilder |
리턴 |
yii\mongodb\LogBuilder |
the log builder for this connection. |
Returns the query builder for the this MongoDB connection.
public getQueryBuilder ( ) : QueryBuilder |
리턴 |
QueryBuilder |
the query builder for the this MongoDB connection. |
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()
공개 메소드
Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
Selects the database with given name.
setDefaultDatabaseName()
공개 메소드
Sets default database name.
Sets log builder used for this connection.
Sets the query builder for the this MongoDB connection.
프로퍼티 상세
$driverOptions 공개적으로 프로퍼티
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 공개적으로 프로퍼티
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.
public $fileStreamProtocol |
$fileStreamWrapperClass 공개적으로 프로퍼티
name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol.
public $fileStreamWrapperClass |
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
]
type map to use for BSON unserialization.
Note: default type map will be automatically merged into this field, possibly overriding user-defined values.