PHP Class yii\mongodb\Migration

Each child class of Migration represents an individual MongoDB migration which is identified by the child class name. Within each migration, the [[up()]] method should be overridden to contain the logic for "upgrading" the database; while the [[down()]] method for the "downgrading" logic. Migration provides a set of convenient methods for manipulating MongoDB data and schema. For example, the Migration::createIndex method can be used to create a collection index. Compared with the same methods in Collection, these methods will display extra information showing the method parameters and execution time, which may be useful when applying migrations.
Since: 2.0
Author: Klimov Paul ([email protected])
Inheritance: extends yii\base\Component, implements yii\db\MigrationInterface
Datei anzeigen Open project: yiisoft/yii2-mongodb Class Usage Examples

Public Properties

Property Type Description
$db the MongoDB connection object or the application component ID of the MongoDB connection that this migration should work with. Starting from version 2.0.2, this can also be a configuration array for creating the object.

Public Methods

Method Description
batchInsert ( array | string $collection, array $rows, array $options = [] ) : array Inserts several new rows into collection.
createCollection ( string | array $collection, array $options = [] ) Creates new collection with the specified options.
createIndex ( string | array $collection, array | string $columns, array $options = [] ) Creates an index on the collection and the specified fields.
createIndexes ( string | array $collection, array $indexes ) Creates indexes in the collection.
dropAllIndexes ( string | array $collection ) Drops all indexes for specified collection.
dropCollection ( string | array $collection ) Drops existing collection.
dropIndex ( string | array $collection, string | array $columns ) Drop indexes for specified column(s).
dropIndexes ( string | array $collection, string $indexes ) Drops collection indexes by name.
init ( ) Initializes the migration.
insert ( array | string $collection, array | object $data, array $options = [] ) : MongoDB\BSON\ObjectID Inserts new data into collection.
remove ( array | string $collection, array $condition = [], array $options = [] ) : integer | boolean Removes data from the collection.
save ( array | string $collection, array | object $data, array $options = [] ) : MongoDB\BSON\ObjectID Update the existing database data, otherwise insert this data
update ( array | string $collection, array $condition, array $newData, array $options = [] ) : integer | boolean Updates the rows, which matches given criteria by given data.

Protected Methods

Method Description
beginProfile ( string $token ) Marks the beginning of a code block for profiling.
composeCollectionLogName ( array | string $collection ) : string Composes string representing collection name.
endProfile ( string $token ) Marks the end of a code block for profiling.
log ( string $string ) Logs the incoming message.

Method Details

batchInsert() public method

Inserts several new rows into collection.
public batchInsert ( array | string $collection, array $rows, array $options = [] ) : array
$collection array | string collection name.
$rows array array of arrays or objects to be inserted.
$options array list of options in format: optionName => optionValue.
return array inserted data, each row will have "_id" key assigned to it.

beginProfile() protected method

Marks the beginning of a code block for profiling.
Since: 2.1.1
protected beginProfile ( string $token )
$token string token for the code block.

composeCollectionLogName() protected method

Composes string representing collection name.
protected composeCollectionLogName ( array | string $collection ) : string
$collection array | string collection name.
return string collection name.

createCollection() public method

Creates new collection with the specified options.
public createCollection ( string | array $collection, array $options = [] )
$collection string | array name of the collection
$options array collection options in format: "name" => "value"

createIndex() public method

Creates an index on the collection and the specified fields.
public createIndex ( string | array $collection, array | string $columns, array $options = [] )
$collection string | array name of the collection
$columns array | string column name or list of column names.
$options array list of options in format: optionName => optionValue.

createIndexes() public method

Creates indexes in the collection.
Since: 2.1
public createIndexes ( string | array $collection, array $indexes )
$collection string | array name of the collection
$indexes array indexes specifications.

dropAllIndexes() public method

Drops all indexes for specified collection.
public dropAllIndexes ( string | array $collection )
$collection string | array name of the collection.

dropCollection() public method

Drops existing collection.
public dropCollection ( string | array $collection )
$collection string | array name of the collection

dropIndex() public method

Drop indexes for specified column(s).
public dropIndex ( string | array $collection, string | array $columns )
$collection string | array name of the collection
$columns string | array column name or list of column names.

dropIndexes() public method

Drops collection indexes by name.
Since: 2.1
public dropIndexes ( string | array $collection, string $indexes )
$collection string | array name of the collection
$indexes string wildcard for name of the indexes to be dropped.

endProfile() protected method

Marks the end of a code block for profiling.
Since: 2.1.1
protected endProfile ( string $token )
$token string token for the code block.

init() public method

This method will set [[db]] to be the 'db' application component, if it is null.
public init ( )

insert() public method

Inserts new data into collection.
public insert ( array | string $collection, array | object $data, array $options = [] ) : MongoDB\BSON\ObjectID
$collection array | string collection name.
$data array | object data to be inserted.
$options array list of options in format: optionName => optionValue.
return MongoDB\BSON\ObjectID new record id instance.

log() protected method

By default this method sends message to 'stdout'.
Since: 2.1.1
protected log ( string $string )
$string string message to be logged.

remove() public method

Removes data from the collection.
public remove ( array | string $collection, array $condition = [], array $options = [] ) : integer | boolean
$collection array | string collection name.
$condition array description of records to remove.
$options array list of options in format: optionName => optionValue.
return integer | boolean number of updated documents or whether operation was successful.

save() public method

Update the existing database data, otherwise insert this data
public save ( array | string $collection, array | object $data, array $options = [] ) : MongoDB\BSON\ObjectID
$collection array | string collection name.
$data array | object data to be updated/inserted.
$options array list of options in format: optionName => optionValue.
return MongoDB\BSON\ObjectID updated/new record id instance.

update() public method

Note: for "multiple" mode Mongo requires explicit strategy "$set" or "$inc" to be specified for the "newData". If no strategy is passed "$set" will be used.
public update ( array | string $collection, array $condition, array $newData, array $options = [] ) : integer | boolean
$collection array | string collection name.
$condition array description of the objects to update.
$newData array the object with which to update the matching records.
$options array list of options in format: optionName => optionValue.
return integer | boolean number of updated documents or whether operation was successful.

Property Details

$db public_oe property

the MongoDB connection object or the application component ID of the MongoDB connection that this migration should work with. Starting from version 2.0.2, this can also be a configuration array for creating the object.
public $db