mongodb 为所有类实现 BsonIgnoreExtraElements
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12944520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement for all classes BsonIgnoreExtraElements
提问by Cleyton Ferrari
I'm using mongDb with MongoDrive, I wonder how I can implement to all my classes the [BsonIgnoreExtraElements].
我正在将 mongDb 与 MongoDrive 一起使用,我想知道如何为我的所有类实现[BsonIgnoreExtraElements]。
I know there is a way through the ConventionProfile, but I do not know how to implement it.
我知道有一种方法可以通过ConventionProfile,但我不知道如何实现它。
回答by McGarnagle
Use the SetIgnoreExtraElementsConventionmethod (from the Conventionssection of the C# Driver Serialization Tutorial):
使用SetIgnoreExtraElementsConvention方法(来自C# 驱动程序序列化教程的约定部分):
var myConventions = new ConventionProfile();
myConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()));
BsonClassMap.RegisterConventions(myConventions, (type) => true);
The parameter (type) => trueis a predicate depending on the class type, that determines whether to apply the convention. So per your requirement it should simply return true regardless; but you could use this to set/exclude the convention on given types if you wanted.
参数(type) => true是一个取决于类类型的谓词,决定是否应用约定。因此,根据您的要求,无论如何它都应该简单地返回 true;但是如果需要,您可以使用它来设置/排除给定类型的约定。
Edit
编辑
Per Evereq's comment, the above is obsolete. Now use:
根据 Evereq 的评论,以上内容已过时。现在使用:
var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
回答by Cleyton Ferrari
Basically,
基本上,
mongoDB affords you the ability to store documents within a single collection that can each have their own schema. This is a drastic change if you have a background in relational databases. In a relational database, you have a static schema per table and you cannot deviate from without changing the structure of the table. In the example below, I have defined a Person class and a PersonWithBirthDate class which derives from the Person class. These can both be stored in the same collection in a mongoDB database. In this case, assume the documents are stored in the Persons collection.
mongoDB 使您能够将文档存储在单个集合中,每个集合都有自己的架构。如果您有关系数据库的背景,这是一个巨大的变化。在关系数据库中,每个表都有一个静态模式,如果不改变表的结构,就不能偏离。在下面的示例中,我定义了一个 Person 类和一个派生自 Person 类的 PersonWithBirthDate 类。它们都可以存储在 mongoDB 数据库的同一个集合中。在这种情况下,假设文档存储在 Persons 集合中。
See the sample code -
查看示例代码 -
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class PersonWithBirthDate : Person
{
public DateTime DateOfBirth { get; set; }
}
Now , You can easily retrieve and of the documents and have the mongoDB C# Driver deserialze the document into the PersonWithBirthDate class. However, you will run into issues if you query the Persons collection and try to have the driver deserialize the data into the Person class. You will receive an error that there are elements that cannot be deserialized. This easily fixable by adding the [BsonIgnoreExtraElements] attribute to the Person class. You can see the modified class below. This will instruct the driver to ignore any elements that it cannot deserialize into a corresponding property. With the attribute any document in the Persons collection can be deserailized to the Person class without error.
现在,您可以轻松地检索文档并让 mongoDB C# 驱动程序将文档反序列化为 PersonWithBirthDate 类。但是,如果您查询 Persons 集合并尝试让驱动程序将数据反序列化到 Person 类中,则会遇到问题。您将收到一条错误消息,指出存在无法反序列化的元素。通过将 [BsonIgnoreExtraElements] 属性添加到 Person 类,可以轻松解决此问题。您可以在下面看到修改后的类。这将指示驱动程序忽略它无法反序列化为相应属性的任何元素。使用属性可以将 Person 集合中的任何文档脱轨到 Person 类而不会出错。
[BsonIgnoreExtraElements]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class PersonWithBirthDate : Person
{
public DateTime DateOfBirth { get; set; }
}
I hope this will clear your understanding.
我希望这能让你明白。

