Skip to content
March 30, 2013 / edeustace

Migrating mongo db

On my current project, we are at a stage where we want to set up a deployment pipeline, from a developer machine to a production environment (with all the steps in between). We are using Heroku for our deployments and a hosted mongo service.

One part of the challenge in setting up this pipeline is migrating the database so that the updated application will work with its data set and schema.

To that end we started looking at mechanisms for migrating Mongo databases. The main requirement was that we could migrate and rollback a database to a target version.

There are some interesting options on github (see below), but they didn’t quite fit into what we wanted. So we’ve created our own library:

https://github.com/corespring/mongo-migrator

We’ve been using it now for the last few months and its working good.

The flow is as follows:

Write a migration/rollback mongo script that has the following format:

var x = "y";
//Up is run on a migration
function up(){ 
  db.item.insert({name: "Ed", surname: "Eustace"})
}
//Down is run on a rolback
function down(){ 
  db.item.remove({ name: "Ed", surname: "Eustace});
}

Save the script to a folder, eg 001_migrate.js

Run mongo-migrator:

mongo-migrator migrate [version_id] mongo_uri script_path_1
//eg
//java -jar mongo-migrator.jar migrate 1 mongodb://localhost/my-db migrations/

The migrator will check what scripts have been already run in this folder and only run ones that haven’t been run yet (it stores these in a collection called ‘mongo_migrator_versions’).

Rollback is the opposite – it runs the down() function in reverse order from the existing scripts in the db.

There’s more information on the project’s readme. Hopefully people find it useful.

Other notable mongo migration projects:

Advertisements

One Comment

Leave a Comment
  1. Abdurrahman Sahin / Feb 4 2015 11:24 am

    Looks nice, much better than available tools, liked the rollback feature.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: