PHP code example of kevinkl3 / php-mongo-migrator

1. Go to this page and download the library: Download kevinkl3/php-mongo-migrator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kevinkl3 / php-mongo-migrator example snippets




class RevisionName extends \Sokil\Mongo\Migrator\AbstractMigration
{
    public function up()
    {
        
    }
    
    public function down()
    {
        
    }
}



class RevisionName extends \Sokil\Mongo\Migrator\AbstractMigration
{
    protected function init(): void
    {
        // some common code
    }
    
    public function up(): void
    {
        $collection = $this->getDatabase('some_database')
            ->selectCollection('some_collection');

        // create new field in all documents of collection
        $collection->updateMany(
            [],
            ['$set' => ['newField' => 'defaultValue']]
        );
    }
    
    public function down(): void
    {
        $collection = $this->getDatabase('some_database')
            ->selectCollection('some_collection');

        // remove field from all documents of collection
        $collection->updateMany(
            [],
            ['$unset' => ['newField' => '']]
        );
    }
}

composer 

pc:~/php-mongo-migrator$ ./bin/mongo-migrator create RevisionName
New migration created at ~/php-mongo-migrator/migrations/20151127093706_RevisionName.php