max2005b
max2005b

Reputation: 373

Handling MongoDB schema changes

How do you deal with schema changes in production?

Let's say i'm tampering with some classes in my C# project, adding a complicated property which consists of a Dictionary (Key(UInt16),Value(UInt16)) while value has a default value, an integer. How would you add this to your current MongoDB instance in production?

C# Example: I'm adding this dictionary to an existing class (which obviously reflects in the MongoDB as a collection):

public Dictionary<SomeEnum, object> SomeSettings = new Dictionary<SomeEnum, object>()
{
 { SomeEnum.BGColor, "#FFFFFF" }, //White
 { SomeEnum.QRColor, "#000000" }, //Black
 { SomeEnum.LogoSize, (UInt16)100 }, //White
 { SomeEnum.MemberDetailsColor, "#000000" }, //Black
 { SomeEnum.BottomTextColor, "#000000" }, //Black
 { SomeEnum.ShowDecreaseButton, true }
}; 

SomeEnum a UInt16 typed enum, this whole dictionary is a brand new field I would like to add to an existing collection, with those default values. How would you act?

Thanks!!!

Upvotes: 1

Views: 410

Answers (1)

LoSciamano
LoSciamano

Reputation: 1119

It seems as a one-time operation so you can do it via mongodb shell. Log to your mongodb server (or RP master, if you're using ReplicaSet).

use your_db
default_dict = {
      uint16_key_a : uint16_value a,
      uint16_key_b : uint16_value b,
      ...
}
db.you_collection.update({},{ "$set" : { "your_brand_new_field" :default_dict} },false,true)

The update command sets the _your_brand_new_field_ field to _default_dict_ for each document ( the {} query matchs everything). Hope this is what you need to know.

Upvotes: 1

Related Questions