Question

I have two projects which are based on mongodb database. One project use csharp-mongodb and another - norm driver.

I make request to my db only with generic methods like GetCollection. How can i add new field to my entity with less painfull?

For example, At the beginning i have

public class MyEntity 
{
     public int _id {get;set;}
     public string Firstname {get;set;}
}

in a few days i decided to add new field

public class MyEntity 
{
     public int _id {get;set;}
     public string Firstname {get;set;}
     public string Lastname {get;set;} //here is
}

I see only one way - make utility which get my entity then deserialize it and convert to new type and then serialize to bson. As for me - it's hard way - on the understanding that i have more than 1 million records.

I know that csharp mongodb driver have the ability to work with documnets :)

Was it helpful?

Solution

If you are using Norm driver, newly added fields automatically updated when insertion and retrieval. You don't have to do manual deserialization.

when retieveing, the new field Lastname will be return as null for old documents. On insertion this new field will be added to the document.

OTHER TIPS

@Antony, do you need to update all records when you get new field? Try to choose lazy updating - update each old record within request. But all new-style records will have all record fields.

I like samus driver. Of course it works with BSON documents, but when you want to update some record MongoDB returns full BSON doc, not the part. I am new in MongoDB, but as i understand MongoDB cannot returns some part of document. It works like your utility: get -> deserialize -> update -> serialize -> save; I do not think that it is hard way when we talk about 1M-10M records;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top