Merge customer database with upgraded version without data loss

Hi All,
We have our database at a specific version lets say 1.0.0, as it deployed in customer environment that hold now customer data.

Now, we want to upgrade customer’s database from 1.0.0 → 1.2.3 (with certain modification in the collection schema’s) without any data loss. Our MongoDB server is locally configured and shipped with the installer.

Few cases which I have identified are:

  • No change in schema:
    This is straightforward and can be achieved by using mongodump/ mongorestore.

  • A column removed or added:
    For this, my observation is this is achievable, just drop the particular column or add new column with empty data.

  • A difficult situation where let suppose in the Customer collection in 2.0.0 we split the column name into first, middle, and last name or move address to another collection, and the client database is at version 1.0.3.

Need help on this. Thanks

Welcome to the MongoDB Community Forums @Nand_Sharma !

It sounds like your database version changes are referring to application schema rather than MongoDB server upgrades. If so, I believe the feature you are describing is “schema migration”, which is not handled directly by the MongoDB server (and strictly speaking, not a requirement for all use cases). However, there are quite a few libraries and language-agnostic schema migration tools.

The general pattern for schema migration tools is:

  • Individual schema migrations have an associated script where you write functions for upgrade (and optionally, downgrade) paths. Developers usually find it convenient to write migration scripts using the same language/driver as their application.

  • Each migration script has a unique ID and is typically named with a convention that ensures migrations are run in a predictable order.

  • Migration scripts can be checked into source control and versioned & packaged with your application release. A database instance could be upgraded to 1.2.3 from any prior version by applying all outstanding migration scripts in a predictable order.

  • The migration tool keeps track of migrations that have already been applied to the local database (usually, in the database itself) so migrations are not applied more than once.

No change in schema

You mentioned mongodump/mongorestore, but if there are no schema changes you could take a mongodump backup but wouldn’t need to restore.

A column removed or added:

This could be done via a migration script, but I’d definitely backup before destructive changes.

A difficult situation where let suppose in the Customer collection in 2.0.0 we split the column name into first, middle, and last name or move address to another collection, and the client database is at version 1.0.3.

This sort of data manipulation could be also done via a migration script.

A basic example of this sort of schema migration tool is mongo-migrate for Node.js.

Regards,
Stennie

1 Like

Thank you very much, @Stennie. This is really very helpful.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.