如何管理不断变化的数据结构MongoDB中集瓦特/ Simple.Data? [英] How can I manage changing data structures in MongoDb collections w/ Simple.Data?

查看:147
本文介绍了如何管理不断变化的数据结构MongoDB中集瓦特/ Simple.Data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们正在使用Simple.Data和MongoDB的适配器。当我们检索到的文献,我们投它变成一个POCO,例如:

We're currently using Simple.Data and the MongoDb adapter. When we've retrieved a document, we cast it into a POCO, e.g:

(User)db.Users.FindById(1234);

首先,这个工作得很好(哎呀呀,没有模式!)。但是,如果我们改变用户对象的结构(例如,添加一个新的领域,或更改字段的数据类型),那么我们可以再施放原始文件,因为它不符合我们的新的阶级结构。

To begin with, this works quite well (heck yeah, no schema!). However, if we change the structure of the User object (e.g. add a new field, or change a field's data type), then we can no longer cast the original document, as it doesn't match our new class structure.

要解决这个问题,到目前为止,我们已经尝试了两个最直接的方法:

To resolve this, we've so far tried the two most straight-forward approaches:

  1. 手动更新数据的变化反映文档结构。好了的那一刻,而不是管理当项目在多个环境中部署/使得它投入生产
  2. 手动映射;例如。铸造SimpleRecord到字典和人工评估的成员。我担心这种方法的性能,虽然没有替补标记它。我还担心,我还没有找到一种方法,使它通用不使用的目标类型反射来确定成员的名称。

我们也已经研究如何这是解决了红宝石和的 Python的。前者呼吁更多的(旧的模式版本在明代的维护似乎像它可能是矫枉过正)。

We've also looked into ways this was solved with Ruby and Python. The former appeals more (the maintenance of old schema versions in Ming seems like it may be overkill).

在我逃跑和港口疯狂的东西,有没有人解决了这个问题,Simple.Data?任何人都可以与文档结构改变经营模式,较少的数据库提供任何指导,最佳做法?

Before I run off and port something crazy, has anyone solved this problem with Simple.Data? Can anyone offer any guidance as to best practices for dealing with document structure changes in schema-less databases?

推荐答案

看通过自定义序列化。这是快的和有用的在我的情况:

Look through custom serialization. It was fast and useful in my case:

public class FieldsWrapper : IBsonSerializable
{
    public List<DataFieldValue> DataFieldValues { get; set; }


    public object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
    {
    if (nominalType != typeof(FieldsWrapper)) throw new ArgumentException("Cannot deserialize anything but self");
    var doc = BsonDocument.ReadFrom(bsonReader);
    var list = new List<DataFieldValue>();
    foreach (var name in doc.Names)
    {
        var val = doc[name];
        if (val.IsString)
            list.Add(new DataFieldValue {LocalIdentifier = name, Values = new List<string> {val.AsString}});
        else if (val.IsBsonArray)
        {
            DataFieldValue df = new DataFieldValue {LocalIdentifier = name};
            foreach (var elem in val.AsBsonArray)
            {
                df.Values.Add(elem.AsString);
            }
            list.Add(df);
        }
    }
    return new FieldsWrapper {DataFieldValues = list};
    }


    public void Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
    {
        if (nominalType != typeof (FieldsWrapper))
            throw new ArgumentException("Cannot serialize anything but self");
        bsonWriter.WriteStartDocument();
        foreach (var dataFieldValue in DataFieldValues)
        {

            bsonWriter.WriteName(dataFieldValue.LocalIdentifier);
            if (dataFieldValue.Values.Count != 1)
            {
                var list = new string[dataFieldValue.Values.Count];
                for (int i = 0; i < dataFieldValue.Values.Count; i++)
                    list[i] = dataFieldValue.Values[i];
                BsonSerializer.Serialize(bsonWriter, list); 
            }
            else
            {
                BsonSerializer.Serialize(bsonWriter, dataFieldValue.Values[0]); 
            }
        }
        bsonWriter.WriteEndDocument();
    }

}

有一些其他的战术,但是这似乎是最适用的一个在您的案件。我们测试的生产字典,他们尽可能快地一切,你只会失去时间的映射,如果字典不是自然的你的域名,我会去自定义序列化。

There are some other tactics but this seems to be the most applicable one in your case. We tested dictionaries in production and they are as fast as everything else, you will only lose time on mapping if dictionary is not natural for your domain, I would go for custom serialization.

这篇关于如何管理不断变化的数据结构MongoDB中集瓦特/ Simple.Data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆