5

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.
Community
  • 1
  • 1
Paul
  • 113
  • 6
  • Not sure I understand: Documents in one mongo collections can be different, so there's nothing stopping you from pushing new documents into existing collection, even though the new documents are based on instances of updated classes. Or is the issue that you want patch up existing documents in the db? Another thing: Is the issue with development machine, or with the production system? – Christian Horsdal Sep 23 '11 at 06:38
  • 3
    @ChristianHorsdal: You are right. there is nothing stopping us to insert new documents;however, when we retrieve the documents the serializer will throw exception. there is workaround available, if we specify to ignore extra elements or collect extra elements in another BSON document. This can be done in the BSON serializer. But we were looking for some sort of pre-build script that will alert the developer to make the changes – Paul Sep 23 '11 at 12:46

1 Answers1

1

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.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • Thanks to this answer I discovered BsonDefaultValueAttribute which is essential for the recommendation above! – kingdango Nov 28 '13 at 01:07