Like most of us, I'm coming from a relational database world, and I'm currently looking into the possibilities of the document database world. One of my concerns is the handling of changes in the data model over time (new properties are added, properties are renamed, relationships are added, ..).
In relational databases, this is typically handled as follows:
When using a document database, I have a feeling that changes to the data model are much easier; there's no need to update a database schema, mostly it's just adding a property, .. and everything "just works". I wonder how teams are managing this kind of migrations in real life, Enterprise projects with document databases:
Thanks for your time,
Koen
With RavenDB, you can do that with patching. See: http://ayende.com/blog/157185/awesome-ravendb-feature-of-the-day-evil-patching And: http://blog.hibernatingrhinos.com/12705/new-option-in-the-ravendb-studiondash-patching
There are three general strategies that you can take for "schema" modifications in MongoDB. I've seen all three work well; which one you'll use depends a lot on your particular use case.
First: you can simply add a new field to new documents, and write your code to handle the case where that field does not exist. For example, you could add an 'address
' field to your "user" documents, but you'd have to write your client code to handle the case where that field does not exist.
Second: you can write your code to look at the existing document & update it when it sees an "old-style" document. For example, you could have code that checks to see if there is a "name
" field in your "user" document. If it finds that field, it would split it up into "first_name
" and "sur_name
" fields, $unset
the "name
" field in that document, and $set
the new "first_name
" and "sur_name
" fields to their calculated values.
Third: you can batch-update all of the documents in the collection to use the new schema. You'd write the same code as above, but instead of lazily applying it when your application reads a document, you'd apply it to all the documents in the collection.
Note that this last strategy can have performance impact: if you page in a lot of documents that haven't been accessed for a while, you'll put an extra load on your MongoDB system.
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