I was reading an old answer which was recently updated, and noticed that the author doesn't quote the simplicity of data migration as a benefit of MongoDB. I always thought that the major benefit of a NoSQL solution such as MongoDB is that it makes migrations extremely simple compared to the usual relational databases.
In fact, in the common relational databases, some changes in the schema, such as adding a column to a table, are simple. However, a lot of other changes (and from my experience, those are the ones which are the most frequent) could be extremely challenging.
Here's an example of such a change. Recently, I got a project which was managed by a different team. In this project, they decided not to use natural keys, and this made any data manipulation very painful. Say I want to know if a given employee has the administrator role. Instead of just querying the
user_role table, filtering by the employee ID, I have to first go to the
user table to find the primary key corresponding to the employee ID, then to the
role table to find the primary key corresponding to the administrator role, and finally
SELECT from the
user_role table, possibly putting the two primary keys in the wrong order and mistakenly thinking that the user is not an administrator, while he actually is.
The solution was to move to natural keys. However, the original primary keys being referenced in other tables, I had to:
- Create extra tables (such as
user2) with the new schema.
- Modify the application so that it handles two tables in parallel.
- Move the data to the new tables.
- Modify the application once again to give preference to new tables.
- Remove the old tables.
- Modify the application to handle the next renaming.
- Rename the new tables (
- Modify the application to forget about
That's... well... a lot of work for a change which looks so simple from a business perspective. If I were using MongoDB, I would simply:
- Change the application to start using natural keys, while still supporting the auto-incremented values.
- Migrate the data by replacing the auto-incremented identifier by the value of a natural key.
- Change the application once more to forget about auto-incremented values.
That's like moving from a few days of work to something which could be performed in less than an hour, with much less risk to make a mistake somewhere in the process.
Am I missing something about the data migration in MongoDB compared to the data migration in most popular relational databases? Or is the simplicity of data migration still a huge benefit of MongoDB?