Document Databases: Data Model Migration

Like most of us, I come from the world of relational databases, and I am now exploring the possibilities of the world of a document database. One of my problems is handling changes to the data model over time (new properties added, properties renamed, relationships added, ..).

In relational databases, this is usually handled as follows:

  • Database Migration Record
    -> Change database schema
    -> Correct data for existing rows (usually contains some business logic)
  • Change the code (ORM updates, ..)


When using a document database, I get the feeling that changes to the data model are much simpler; there is no need to update the database schema, basically it's just adding a property, and everything "just works." It is interesting how teams manage these types of migration in real life, Enterprise projects with document databases:

  • Is there a strict policy for modifying types stored in a db document? For example, each change of this type requires a transition to updating existing documents?
  • As a result, is there a clear separation between the data model (types stored in the db document) and the business model?

Thank you for your time,
Koen

+4
source share
2 answers

There are three general strategies that you can use to modify schemas in MongoDB. I saw that all three work well; which you will use depends on your specific use case.

First: you can simply add a new field to new documents and write your code to handle the case when this field does not exist. For example, you can add the " address " field to your "user" documents, but you will have to write a client code to handle the case when this field does not exist.

Secondly: you can write your code to look at an existing document and update it when it sees a "old style" document. For example, you might have code that checks to see if there is a " name " field in your "user" document. If he finds this field, he will divide it into the fields " first_name " and " sur_name ", the $unset field " name " in this document and $set new " first_name ", and " sur_name " to their calculated values.

Third: you can batch update all documents in the collection to use the new scheme. You should write the same code as above, but instead of lazily applying it when your application reads a document, you should apply it to all documents in the collection.

Please note that this last strategy may have a performance impact: if you download a lot of documents that have not been available for a while, you will add extra workload to your MongoDB system.

+1
source

All Articles