无Mongo模式的集合C# [英] Mongo Schema-less Collections & C#

查看:55
本文介绍了无Mongo模式的集合C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索Mongo作为关系数据库的替代方案,但是我遇到了无模式集合概念的问题.

I'm exploring Mongo as an alternative to relational databases but I'm running into a problem with the concept of schemaless collections.

从理论上讲,这听起来不错,但是只要将模型绑定到集合,该模型就会成为您的实际架构.您不能再只是从模型中添加或删除字段并期望它继续工作.我在这里看到的问题与管理关系数据库时遇到的问题相同,因为您需要某种脚本才能从数据库模式的一个版本迁移到另一个版本.

In theory it sounds great, but as soon as you tie a model to a collection, the model becomes your defacto schema. You can no longer just add or remove fields from your model and expect it to continue to work. I see the same problems here managing changes as you have with a relational database in that you need some sort of script to migrate from one version of the database schema to the other.

我是从错误的角度出发吗?成员在更新其域模型时采取什么方法来确保其收集项目与域模型保持同步?

Am I approaching this from the wrong angle? What approaches do members here take to ensure that their collection items stay in sync with their domain model when making updates to their domain model?

值得注意的是,这些问题显然也存在于关系数据库中,但是我特别要求使用无模式数据库(尤其是Mongo)来缓解该问题的策略.谢谢!

It's worth noting that these problems obviously exist in relational databases as well, but I'm asking specifically for strategies in mitigating the problem using schemaless databases and more specifically Mongo. Thanks!

推荐答案

使用MongoDB进行架构迁移实际上要比使用SQL Server麻烦得多.

Schema migration with MongoDB is actually a lot less painful than with, say, SQL server.

添加新字段很容易,旧记录会设置为null,或者您可以使用属性来控制默认值[BsonDefaultValue("abc", SerializeDefaultValue = false)]

Adding a new field is easy, old records will come in with it set to null or you can use attributes to control the default value [BsonDefaultValue("abc", SerializeDefaultValue = false)]

[BsonIgnoreIfNull]属性对于在序列化文档时省略文档中为空的对象也很方便.

The [BsonIgnoreIfNull] attribute is also handy for omitting objects that are null from the document when it is serialized.

删除字段也非常容易,您可以使用[BSonExtraElements](请参见 docs )将其收集起来并保存起来,或者您可以使用[BsonIgnoreExtraElements]将其丢弃.

Removing a field is fairly easy too, you can use [BSonExtraElements] (see docs) to collect them up and preserve them or you can use [BsonIgnoreExtraElements] to simply throw them away.

有了这些功能后,实际上就无需将每条记录转换为新的架构了,您可以在记录更新时根据需要懒惰地执行,或者在后台缓慢进行.

With these in place there really is no need to go convert every record to the new schema, you can do it lazily as needed when records are updated, or slowly in the background.

PS,因为您也对在Mongo中使用动态功能感兴趣,所以这是 serializer和动态对象的反序列化器.

PS, since you are also interested in using dynamic with Mongo, here's an experiment I tried along those lines. And here's an updated post with a complete serializer and deserializer for dynamic objects.

这篇关于无Mongo模式的集合C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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