# Abstract Class yii\mongodb\Migration

Inheritance | yii\mongodb\Migration » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable, yii\db\MigrationInterface
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/Migration.php

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 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 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

Property | Type | Description | Defined By
--- | --- | --- | ---
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$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

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\Component
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component
__construct() | Constructor. | yii\base\BaseObject
__get() | Returns the value of a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
attachBehavior() | Attaches a behavior to this component. | yii\base\Component
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component
batchInsert() | Inserts several new rows into collection. | yii\mongodb\Migration
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
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
detachBehavior() | Detaches a behavior from the component. | yii\base\Component
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component
down() | This method contains the logic to be executed when removing this migration. | yii\db\MigrationInterface
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
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
init() | Initializes the migration. | yii\mongodb\Migration
insert() | Inserts new data into collection. | yii\mongodb\Migration
off() | Detaches an existing event handler from this component. | yii\base\Component
on() | Attaches an event handler to an event. | yii\base\Component
remove() | Removes data from the collection. | yii\mongodb\Migration
save() | Update the existing database data, otherwise insert this data | yii\mongodb\Migration
trigger() | Triggers an event. | yii\base\Component
up() | This method contains the logic to be executed when applying this migration. | yii\db\MigrationInterface
update() | Updates the rows, which matches given criteria by given data. | yii\mongodb\Migration

## Protected Methods

Hide inherited methods

Method | Description | Defined 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.

public yii\mongodb\Connection|array|string $db = 'mongodb'

## 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.