Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling migrations with MongoDb

Just to give a little more context to the question, I have a web application (asp mvc) which basically wraps CRUD operations to a MongoDb instance, it carries out validation and certain business logic before the model is verified and sent over to be stored, retrieved etc.

Now one problem we have his is that in the new version the models have changed but the existing data has not, here is an example: (it is c# specific but the question really is language agnostic)

public class Person
{
    public Guid Id {get; set;}
    public string Name {get; set;}
    public int Age {get;set;}
    public string BadgeNo {get;set;}
}

public class Person
{
    public Guid Id {get; set;}
    public string Name {get; set;}
    public int Age {get;set;}
    public string EmployeeNo {get; set;} // Still contains same data as BadgeNo just called something different
}

As you can see the structure of the objects have changed but in Mongo land it is still passing out a BadgeNo, not an EmployeeNo. In an SQL land we would usually have a migration script which is run as part of the build script which would change the schema and update/insert/delete any additional data for that delta.

So how is it best to manage these sort of migrations with Mongo? Should I also have a script which I use to update all instances within Mongo? or is there some other preferred practise for doing this sort of thing.

Any advice on the subject would be great

=== Edit ===

It seems like currently I am wanting to go with the migration option rather than a phasing out approach, so with this in mind can anyone recommend any tools for helping in this area, as otherwise each migration (assuming a roll-in, roll-out) would have to be a pre compiled assembly of some kind with all the logic in. I was thinking something along the lines of FluentMigrator but instead of working with SQL you are working with Mongo. Currently my build scripts are using Nant, I have seen some ruby tools but not sure if there are any .net equivalent.

like image 578
Grofit Avatar asked Feb 17 '12 10:02

Grofit


People also ask

Are there migrations in MongoDB?

Based on Java, Mongock is a MongoDB Migration Tool that migrates data and ships database changes to the application via your application code for 'distributed environments.

What are the 3 main DB migration strategies?

There are three main approaches to database migration: big bang data migration, trickle data migration, and zero downtime migration.

What is DB migration in MongoDB?

Mongo DB migration is the process of migrating and transferring all the data from the source database to the target database which indeed is going to be a Mongo DB database.

How do I create a migration file in MongoDB?

To create a new database migration script, just run the migrate-mongo create [description] command. A new migration file is created in the 'migrations' directory: module. exports = { up(db, client) { // TODO write your migration here.


1 Answers

There are basically two approaches:

  1. Make sure that your application code can handle both "versions" of the data structure, and when saving, updates to the new structure
  2. Write a migration script

I would probably go for option 1 as it's the method that allows you to gradually update, where as with option 2 you basically need to take down your application so that you can update the code (fast) and data (possibly slower) in one go.

Then later, or if you find it necessary do option 2 as well to migrate your data over. This then doesn't have to take down your site, and can happily run asynchronously in the background.

like image 199
Derick Avatar answered Sep 21 '22 04:09

Derick