Abstract Class yii\mongodb\Migration

Inheritanceyii\mongodb\Migration » yii\base\Component
Implementsyii\db\MigrationInterface
Available since extension's version2.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
$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
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
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
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

Method Description Defined By
composeCollectionLogName() Composes string representing collection name. yii\mongodb\Migration

Property Details

Hide inherited properties

$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

Hide inherited methods

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.

                public function batchInsert($collection, $rows, $options = [])
{
    echo "    > insert into " . $this->composeCollectionLogName($collection) . ") ...";
    $time = microtime(true);
    $rows = $this->db->getCollection($collection)->batchInsert($rows, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    return $rows;
}

            
composeCollectionLogName() protected method

Composes string representing collection name.

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

Collection name.

return string

Collection name.

                protected function composeCollectionLogName($collection)
{
    if (is_array($collection)) {
        list($database, $collection) = $collection;
        return $database . '.' . $collection;
    } else {
        return $collection;
    }
}

            
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"

                public function createCollection($collection, $options = [])
{
    if (is_array($collection)) {
        list($database, $collectionName) = $collection;
    } else {
        $database = null;
        $collectionName = $collection;
    }
    echo "    > create collection " . $this->composeCollectionLogName($collection) . " ...";
    $time = microtime(true);
    $this->db->getDatabase($database)->createCollection($collectionName, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}

            
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.

                public function createIndex($collection, $columns, $options = [])
{
    echo "    > create index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array) $columns) . empty($options) ? "" : ", " . Json::encode($options) . ") ...";
    $time = microtime(true);
    $this->db->getCollection($collection)->createIndex($columns, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}

            
dropAllIndexes() public method

Drops all indexes for specified collection.

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

Name of the collection.

                public function dropAllIndexes($collection)
{
    echo "    > drop all indexes on " . $this->composeCollectionLogName($collection) . ") ...";
    $time = microtime(true);
    $this->db->getCollection($collection)->dropAllIndexes();
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}

            
dropCollection() public method

Drops existing collection.

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

Name of the collection

                public function dropCollection($collection)
{
    echo "    > drop collection " . $this->composeCollectionLogName($collection) . " ...";
    $time = microtime(true);
    $this->db->getCollection($collection)->drop();
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}

            
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.

                public function dropIndex($collection, $columns)
{
    echo "    > drop index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array) $columns) . ") ...";
    $time = microtime(true);
    $this->db->getCollection($collection)->dropIndex($columns);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}

            
init() public method

Initializes the migration.

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

public void init ( )

                public function init()
{
    parent::init();
    $this->db = Instance::ensure($this->db, Connection::className());
}

            
insert() public method

Inserts new data into collection.

public \MongoId 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 \MongoId

New record id instance.

                public function insert($collection, $data, $options = [])
{
    echo "    > insert into " . $this->composeCollectionLogName($collection) . ") ...";
    $time = microtime(true);
    $id = $this->db->getCollection($collection)->insert($data, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    return $id;
}

            
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.

                public function remove($collection, $condition = [], $options = [])
{
    echo "    > remove " . $this->composeCollectionLogName($collection) . ") ...";
    $time = microtime(true);
    $result = $this->db->getCollection($collection)->remove($condition, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    return $result;
}

            
save() public method

Update the existing database data, otherwise insert this data

public \MongoId 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 \MongoId

Updated/new record id instance.

                public function save($collection, $data, $options = [])
{
    echo "    > save " . $this->composeCollectionLogName($collection) . ") ...";
    $time = microtime(true);
    $id = $this->db->getCollection($collection)->save($data, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    return $id;
}

            
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.

                public function update($collection, $condition, $newData, $options = [])
{
    echo "    > update " . $this->composeCollectionLogName($collection) . ") ...";
    $time = microtime(true);
    $result = $this->db->getCollection($collection)->update($condition, $newData, $options);
    echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    return $result;
}