PHPackages                             salim/mongodb-migrations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. salim/mongodb-migrations

ActiveLibrary[Database &amp; ORM](/categories/database)

salim/mongodb-migrations
========================

Managed Database Migrations for MongoDB

00PHP

Since Aug 8Pushed 7y ago1 watchersCompare

[ Source](https://github.com/MurtazalievSalim/mongodb-migrations)[ Packagist](https://packagist.org/packages/salim/mongodb-migrations)[ RSS](/packages/salim-mongodb-migrations/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

antimattr-mongodb-migrations
============================

[](#antimattr-mongodb-migrations)

The AntiMattr MongoDB Migration library provides managed migration support for MongoDB.

Are you familiar with [Doctrine Migrations](https://github.com/doctrine/migrations)?

This library intentionally parallels the structure and features provided.

Installation
============

[](#installation)

Use composer to install

```
composer install
```

Features
========

[](#features)

Features - Configuration
------------------------

[](#features---configuration)

Similar to [Doctrine Migrations](https://github.com/doctrine/migrations), configurations are separated into 2 files

- Connection configuration (php)
- Migration configuration (xml or yaml)

Example Connection configuration "test\_antimattr\_mongodb.php"

```
/**
 * @link http://php.net/manual/en/mongoclient.construct.php
 */
return array(
    'host' => 'localhost', // default is localhost
    'port' => '27017', // default is 27017
    'dbname' => null, // optional, if authentication DB is required
    'user' => null, // optional, if authentication is required
    'password' => null, // optional, if authentication is required
    'options' => array(
        'connect' => true // recommended
    )
);
```

XML or YAML Migration Configurations are supported

Example XML "test\_antimattr\_mongodb.xml"

```

    AntiMattr Sandbox Migrations
    AntiMattrMigrationsTest

    /path/to/migrations/classes/AntiMattrMigrations

    /path/to/migrations/script_directory

```

Example YAML "test\_antimattr\_mongodb.yml"

```
---
name: AntiMattr Sandbox Migrations
migrations_namespace: AntiMattrMigrationsTest
database: test_antimattr_migrations
collection_name: antimattr_migration_versions_test
migrations_directory: /path/to/migrations/classes/AntiMattrMigrations
migrations_script_directory: /path/to/migrations/script_directory # optional
```

Features - Console Command Support
----------------------------------

[](#features---console-command-support)

There is an example Console Application in the demo directory

This is how to register the commands in your application

```
require '../../vendor/autoload.php';

error_reporting(E_ALL & ~E_NOTICE);

use AntiMattr\MongoDB\Migrations\Tools\Console\Command as AntiMattr;
use Symfony\Component\Console\Application;

$application = new Application();
$application->addCommands(array(
    new AntiMattr\ExecuteCommand(),
    new AntiMattr\GenerateCommand(),
    new AntiMattr\MigrateCommand(),
    new AntiMattr\StatusCommand(),
    new AntiMattr\VersionCommand()
));
$application->run();
```

Notice the console is executable

```
> cd demo/ConsoleApplication/
> ./console
Console Tool

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  --version        -V Display this application version.
  --ansi              Force ANSI output.
  --no-ansi           Disable ANSI output.
  --no-interaction -n Do not ask any interactive question.

Available commands:
  help                          Displays help for a command
  list                          Lists commands
mongodb
  mongodb:migrations:execute    Execute a single migration version up or down manually.
  mongodb:migrations:generate   Generate a blank migration class.
  mongodb:migrations:migrate    Execute a migration to a specified version or the latest available version.
  mongodb:migrations:status     View the status of a set of migrations.
  mongodb:migrations:version    Manually add and delete migration versions from the version table.
```

Features - Generate a New Migration
-----------------------------------

[](#features---generate-a-new-migration)

```
> ./console mongodb:migrations:generate --db-configuration=config/test_antimattr_mongodb.php --configuration=config/test_antimattr_mongodb.yml
Generated new migration class to "Example/Migrations/TestAntiMattr/MongoDB/Version20140822185742.php"
```

Features - Status of Migrations
-------------------------------

[](#features---status-of-migrations)

```
> ./console mongodb:migrations:status --db-configuration=config/test_antimattr_mongodb.php --configuration=config/test_antimattr_mongodb.yml

 == Configuration

    >> Name:                                AntiMattr Example Migrations
    >> Database Driver:                     MongoDB
    >> Database Name:                       test_antimattr_migrations
    >> Configuration Source:                demo/ConsoleApplication/config/test_antimattr_mongodb.yml
    >> Version Collection Name:             migration_versions
    >> Migrations Namespace:                Example\Migrations\TestAntiMattr\MongoDB
    >> Migrations Directory:                Example/Migrations/TestAntiMattr/MongoDB
    >> Current Version:                     0
    >> Latest Version:                      2014-08-22 18:57:44 (20140822185744)
    >> Executed Migrations:                 0
    >> Executed Unavailable Migrations:     0
    >> Available Migrations:                3
    >> New Migrations:                      3
```

Features - Migrate all Migrations
---------------------------------

[](#features---migrate-all-migrations)

This is what you will execute during your deployment process.

```
./console mongodb:migrations:migrate --db-configuration=config/test_antimattr_mongodb.php --configuration=config/test_antimattr_mongodb.yml

                    AntiMattr Example Migrations

WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/n)y
Migrating up to 20140822185744 from 0

  ++ migrating 20140822185742

     Collection test_a

     metric           before               after                difference
     ================================================================================
     count            100                  100                  0
     size             20452                20452                0
     avgObjSize       204.52               204.52               0
     storageSize      61440                61440                0
     numExtents       2                    2                    0
     nindexes         1                    2                    1
     lastExtentSize   49152                49152                0
     paddingFactor    1                    1                    0
     totalIndexSize   8176                 16352                8176

  ++ migrated (0.03s)

  ++ migrating 20140822185743

  ++ migrated (0s)

  ++ migrating 20140822185744

  ++ migrated (0s)

  ------------------------

  ++ finished in 0.03
  ++ 3 migrations executed
```

Features - Execute a Single Migration
-------------------------------------

[](#features---execute-a-single-migration)

```
./console mongodb:migrations:execute --db-configuration=config/test_antimattr_mongodb.php --configuration=config/test_antimattr_mongodb.yml 20140822185742
WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/n)y

  ++ migrating 20140822185742

     Collection test_a

     metric           before               after                difference
     ================================================================================
     count            100                  100                  0
     size             20620                20620                0
     avgObjSize       206.2                206.2                0
     storageSize      61440                61440                0
     numExtents       2                    2                    0
     nindexes         1                    2                    1
     lastExtentSize   49152                49152                0
     paddingFactor    1                    1                    0
     totalIndexSize   8176                 16352                8176

  ++ migrated (0.02s)
```

Features - Version Up or Down
-----------------------------

[](#features---version-up-or-down)

Is your migration history out of sync for some reason? You can manually add or remove a record from the history without running the underlying migration.

You can delete

```
./console mongodb:migrations:version --db-configuration=config/test_antimattr_mongodb.php --configuration=config/test_antimattr_mongodb.yml --delete 20140822185744
```

You can add

```
./console mongodb:migrations:version --db-configuration=config/test_antimattr_mongodb.php --configuration=config/test_antimattr_mongodb.yml --add 20140822185744
```

Features - Analyze Migrations
-----------------------------

[](#features---analyze-migrations)

Identify the collections you want to analyze. Statistics will be captured before and after the migration is run.

```
class Version20140822185742 extends AbstractMigration
{
    public function up(Database $db)
    {
        $testA = $db->selectCollection('test_a');
        $this->analyze($testA);

        // Do the migration
    }
```

Features - Execute JS Scripts
-----------------------------

[](#features---execute-js-scripts)

First identify the directory for scripts in your Migration configuration

```
---
name: AntiMattr Sandbox Migrations
migrations_namespace: AntiMattrMigrationsTest
database: test_antimattr_migrations
collection_name: antimattr_migration_versions_test
migrations_directory: /path/to/migrations/classes/AntiMattrMigrations
migrations_script_directory: /path/to/migrations/script_directory # optional
```

Then execute the scripts via AbstractMigration::executeScripts

```
class Version20140822185743 extends AbstractMigration
{
    public function up(Database $db)
    {
        $result = $this->executeScript($db, 'test_script.js');
    }
```

Pull Requests
=============

[](#pull-requests)

Pull Requests - PSR Standards
-----------------------------

[](#pull-requests---psr-standards)

Please use the pre-commit hook to run the fix all code to PSR standards

Install once with

```
./bin/install.sh
Copying /antimattr-mongodb-migrations/bin/pre-commit.sh -> /antimattr-mongodb-migrations/bin/../.git/hooks/pre-commit
```

Pull Requests - Testing
-----------------------

[](#pull-requests---testing)

Please make sure tests pass

```
$ vendor/bin/phpunit tests
```

Pull Requests - Code Sniffer and Fixer
--------------------------------------

[](#pull-requests---code-sniffer-and-fixer)

Don't have the pre-commit hook running, please make sure to run the fixer/sniffer manually

```
$ vendor/bin/php-cs-fixer fix src/
$ vendor/bin/php-cs-fixer fix tests/
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/3887aaae24479ccc157ea7848c96527beb987dcf8012e9f944016faabc65d50d?d=identicon)[MurtazalievSalim](/maintainers/MurtazalievSalim)

---

Top Contributors

[![MurtazalievSalim](https://avatars.githubusercontent.com/u/1771515?v=4)](https://github.com/MurtazalievSalim "MurtazalievSalim (2 commits)")

### Embed Badge

![Health badge](/badges/salim-mongodb-migrations/health.svg)

```
[![Health](https://phpackages.com/badges/salim-mongodb-migrations/health.svg)](https://phpackages.com/packages/salim-mongodb-migrations)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
