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.
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.
There are three main approaches to database migration: big bang data migration, trickle data migration, and zero downtime migration.
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.
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.
There are basically two approaches:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With