开发者

how to add new field in mongodb, if i use norm or csharp-mongodb drivers

开发者 https://www.devze.com 2023-01-29 10:49 出处:网络
I have two projects which are based on mongodb database. One project use csharp-mongodb and another - norm driver.

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 bs开发者_如何学Con. 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 :)


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.


@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;)

0

精彩评论

暂无评论...
验证码 换一张
取 消