Abstract Class yii\mongodb\Migration

Inheritanceyii\mongodb\Migration » yii\base\Component
Implementsyii\db\MigrationInterface
Available since version2.0

Migration is the base class for representing a MongoDB migration.

Each child class of Migration represents an individual MongoDB migration which is identified by the child class name.

Within each migration, the \yii\mongodb\up() method should be overridden to contain the logic for "upgrading" the database; while the \yii\mongodb\down() method for the "downgrading" logic.

Migration provides a set of convenient methods for manipulating MongoDB data and schema. For example, the createIndex() method can be used to create a collection index. Compared with the same methods in yii\mongodb\Collection, these methods will display extra information showing the method parameters and execution time, which may be useful when applying migrations.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$compact boolean Indicates whether the log output should be compacted. yii\mongodb\Migration
$db yii\mongodb\Connection|array|string The MongoDB connection object or the application component ID of the MongoDB connection that this migration should work with. yii\mongodb\Migration

Public Methods

Hide inherited methods

MethodDescriptionDefined By
batchInsert() Inserts several new rows into collection. yii\mongodb\Migration
createCollection() Creates new collection with the specified options. yii\mongodb\Migration
createIndex() Creates an index on the collection and the specified fields. yii\mongodb\Migration
createIndexes() Creates indexes in the collection. yii\mongodb\Migration
dropAllIndexes() Drops all indexes for specified collection. yii\mongodb\Migration
dropCollection() Drops existing collection. yii\mongodb\Migration
dropIndex() Drop indexes for specified column(s). yii\mongodb\Migration
dropIndexes() Drops collection indexes by name. yii\mongodb\Migration
init() Initializes the migration. yii\mongodb\Migration
insert() Inserts new data into collection. yii\mongodb\Migration
remove() Removes data from the collection. yii\mongodb\Migration
save() Update the existing database data, otherwise insert this data yii\mongodb\Migration
update() Updates the rows, which matches given criteria by given data. yii\mongodb\Migration

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
beginProfile() Marks the beginning of a code block for profiling. yii\mongodb\Migration
composeCollectionLogName() Composes string representing collection name. yii\mongodb\Migration
endProfile() Marks the end of a code block for profiling. yii\mongodb\Migration
log() Logs the incoming message. yii\mongodb\Migration

Property Details

$compact public property (available since version 2.1.5)

Indicates whether the log output should be compacted. If this is set to true, the individual commands ran within the migration will not be output to the console log. Default is false, in other words the output is fully verbose by default.

public boolean $compact false
$db public 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.

Method Details

batchInsert() public method

Inserts several new rows into collection.

public array batchInsert ( $collection, $rows, $options = [] )
$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 (available since version 2.1.1)

Marks the beginning of a code block for profiling.

protected void beginProfile ( $token )
$token string

Token for the code block.

composeCollectionLogName() protected method

Composes string representing collection name.

protected string composeCollectionLogName ( $collection )
$collection array|string

Collection name.

return string

Collection name.

createCollection() public method

Creates new collection with the specified options.

public void createCollection ( $collection, $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 void createIndex ( $collection, $columns, $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 (available since version 2.1)

Creates indexes in the collection.

public void createIndexes ( $collection, $indexes )
$collection string|array

Name of the collection

$indexes array

Indexes specifications.

dropAllIndexes() public method

Drops all indexes for specified collection.

public void dropAllIndexes ( $collection )
$collection string|array

Name of the collection.

dropCollection() public method

Drops existing collection.

public void dropCollection ( $collection )
$collection string|array

Name of the collection

dropIndex() public method

Drop indexes for specified column(s).

public void dropIndex ( $collection, $columns )
$collection string|array

Name of the collection

$columns string|array

Column name or list of column names.

dropIndexes() public method (available since version 2.1)

Drops collection indexes by name.

public void dropIndexes ( $collection, $indexes )
$collection string|array

Name of the collection

$indexes string

Wildcard for name of the indexes to be dropped.

endProfile() protected method (available since version 2.1.1)

Marks the end of a code block for profiling.

protected void endProfile ( $token )
$token string

Token for the code block.

init() public method

Initializes the migration.

This method will set $db to be the 'db' application component, if it is null.

public void init ( )
insert() public method

Inserts new data into collection.

public \MongoDB\BSON\ObjectID insert ( $collection, $data, $options = [] )
$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 (available since version 2.1.1)

Logs the incoming message.

By default this method sends message to 'stdout'.

protected void log ( $string )
$string string

Message to be logged.

remove() public method

Removes data from the collection.

public integer|boolean remove ( $collection, $condition = [], $options = [] )
$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 \MongoDB\BSON\ObjectID save ( $collection, $data, $options = [] )
$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

Updates the rows, which matches given criteria by given data.

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 integer|boolean update ( $collection, $condition, $newData, $options = [] )
$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.