Ignoring Extra Elements in mongoDB C# Driver
MongoDB删除字段后会报错: Element ... does not match any field or property of class Customer.
需要在实体类增加 [BsonIgnoreExtraElements] //using MongoDB.Bson.Serialization.Attributes;
参考这篇老外的文章:
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.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
} public class PersonWithBirthDate : Person
{
public DateTime DateOfBirth { get; set; }
}
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 thePerson class without error.
[BsonIgnoreExtraElements]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
} public class PersonWithBirthDate : Person
{
public DateTime DateOfBirth { get; set; }
}
There is a gotcha that I recently found while trying to implement a scenario similar to the one above. In the source code for the mongo C# driver, the attribute is defined in a way that it can be inherited to the child classes when applied to the parent class (BsonIgnoreExtraElementsAttribute.cs). However, when the attribute is read, it ignores the inheritance of the attribute (BsonClassMap.cs) and does not get applied to the child classes. I agree with this implementation, but it’s a little confusing if you review the source code for the definition of the[BsonIgnoreExtraElements] attribute. Even with this inconsistency, all you need to do is to apply the[BsonIgnoreExtraElements] to each class that may read a document from a collection where there are extra elements.
Ignoring Extra Elements in mongoDB C# Driver的更多相关文章
- Mongoose vs mongodb native driver – what to prefer?
Paul Shan 7th Jun 2015 Mongoose or mongodb native driver, which one to use? This is one of the ini ...
- MongoDB Java Driver操作指南
MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重 ...
- MongoDB C Driver使用教程
MongoDB C Driver使用教程 转载请注明出处http://www.cnblogs.com/oloroso/ 本指南提供简介 MongoDB C 驱动程序. 在 C API 的详细信息,请参 ...
- windows平台下安装、编译、使用mongodb C++ driver
本博客将记录在Win8.1 ,VS2013环境下编译.配置mongodb C++ driver的流程. 1.下载预备 下载Boost:http://sourceforge.net/projects/b ...
- mongodb .net driver
1.介绍 The official MongoDB .NET Driver provides asynchronous interaction with MongoDB. Powering the d ...
- Mongodb Java Driver 参数配置解析
要正确使用Mongodb Java Driver,MongoClientOptions参数配置对数据库访问的并发性能影响极大. connectionsPerHost:与目标数据库能够建立的最大conn ...
- mongodb c++ driver(2.53)windows编译
编译环境: (1) 下载python2.7, 使用x86_32位,因为scons只有32位安装包可用: (2) 下载scons2.3.0,The current production release ...
- MongoDB C Driver and APIinstances linux MongoDB安装配置
<一,linux平台MongoDB安装配置>在这我们使用的Centos6 yum部署的,你想搞编译,自个干!
- MongoDB C driver API continues
开篇前 <1,mongoc_init() func> mongoc_init() Synopsis void mongoc_init (void); Description This fu ...
随机推荐
- 利用Hive实现求两条相邻数据时间差
1.Hive row_number() 函数的高级用法 row_num 按照某个字段分区显示第几条数据 select imei,ts,fuel_instant,gps_longitude,gps_la ...
- Windows 8 系统安装
系统城 http://www.xitongcheng.com/win8/ 1. 下载 win8: http://msdn.itellyou.cn/2. 准备 4G 以上 U 盘,下载 win8 ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- sed 格式化输出df -h
df -h|sed '1d;/ /!N;s/\n//;s/ \+/ /;' 1d——————删除第一行 / /!N——————没有空格的行执行N 例子中没有空格的行 /dev/mapper/vg_ds ...
- 【noiOJ】p7940
01:查找最接近的元素 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个非降序列中,查找与给定值最接近的元素. 输入 第一行包含一个整数n,为非降序列长度.1 <= n ...
- FS210开发板上Qt4.7.0移植过程
作者:冯老师,华清远见嵌入式学院讲师. 1. 搭建Qt开发环境平台 1.开发环境:ubuntu 12.04 2.交叉编译链:arm-cortex_a8-linux-gnueabi 3.开发板:FS21 ...
- Linux open函数
Linux open函数 open 函数用于打开和创建文件.以下是 open 函数的简单描述 #include <fcntl.h> int open(const char *pathnam ...
- C#中Post和Get提交
1.Post提交 private string PostWebRequest(string Url, string paramData, string dataEncode) { string ret ...
- Connect is a middleware layer for Node.js
Connect is a middleware layer for Node.js http://senchalabs.github.com/connect Connect Connect is ...
- nodejs入门(一)
1.nodejs简介 Nodejs不是一个js应用.而是一个js运行平台. Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效 2. 1).Nodejs内置了一个HTTP模块 var ...