Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing changes in class structure to be consistent with mongodb collection

We are using mongodb with c#. We are trying to figure out a way to keep our collection consistent seamlessly. Right now, if a developer make any changes to the class structure(add a field or change data type or changing the property within a nested class) he/she has to change the mongo collection manually.

Its a pain as our project is growing and the developers working on the project keeps increasing. Was wondering whether someone already have figured out a way to manage this issue.

  • Research
    1. I found a similar question. however, couldn't find the solution.
    2. Found a way to find all properties Finding the properties; however, datatype and nested documents becomes an issue.
like image 358
Paul Avatar asked Nov 13 '22 16:11

Paul


1 Answers

If you want to migrate gradually as records are accessed you need to follow a few simple rules:

1) If you add a field it had better be nullable or have a default value specified.

2) Never rename fields, never change field types
- Instead always add new fields, add migration code, remove the old fields only when all documents have been migrated over.

For prototyping with MongoDB and C# I build a dynamic wrapper ... that lets you specify your objects using only interfaces (no classes needed), and it lets you dynamically add new interfaces to an existing object. Not ready for production use but for prototyping it saves a lot of effort and makes migration really easy.

like image 96
Ian Mercer Avatar answered Dec 23 '22 16:12

Ian Mercer