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的更多相关文章

  1. 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 ...

  2. MongoDB Java Driver操作指南

    MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重 ...

  3. MongoDB C Driver使用教程

    MongoDB C Driver使用教程 转载请注明出处http://www.cnblogs.com/oloroso/ 本指南提供简介 MongoDB C 驱动程序. 在 C API 的详细信息,请参 ...

  4. windows平台下安装、编译、使用mongodb C++ driver

    本博客将记录在Win8.1 ,VS2013环境下编译.配置mongodb C++ driver的流程. 1.下载预备 下载Boost:http://sourceforge.net/projects/b ...

  5. mongodb .net driver

    1.介绍 The official MongoDB .NET Driver provides asynchronous interaction with MongoDB. Powering the d ...

  6. Mongodb Java Driver 参数配置解析

    要正确使用Mongodb Java Driver,MongoClientOptions参数配置对数据库访问的并发性能影响极大. connectionsPerHost:与目标数据库能够建立的最大conn ...

  7. mongodb c++ driver(2.53)windows编译

    编译环境: (1) 下载python2.7, 使用x86_32位,因为scons只有32位安装包可用: (2) 下载scons2.3.0,The current production release ...

  8. MongoDB C Driver and APIinstances linux MongoDB安装配置

    <一,linux平台MongoDB安装配置>在这我们使用的Centos6 yum部署的,你想搞编译,自个干!

  9. MongoDB C driver API continues

    开篇前 <1,mongoc_init() func> mongoc_init() Synopsis void mongoc_init (void); Description This fu ...

随机推荐

  1. Spring mvc 验证码的做法

    http://jingyan.baidu.com/article/4f7d5712da7a131a201927b0.html

  2. 记一次Runtime的巧用

    背景 我们的视频直播是用的大华乐橙的解决方案,而他们近期出来个新的SDK,并且对老版SDK不兼容,而这周,终于把大华乐橙的新版SDK切换了,和这一周做的新的东西,一起提交审核了,并且今天也通过审核了. ...

  3. MFC 打开其他程序

    WinExec("程序路径",SW_SHOW);

  4. Extjs用到的一些有用的网页的集锦

    1. extJS常用的4中Ajax异步提交 http://www.2cto.com/kf/201403/284083.html http://blog.csdn.net/goodshot/articl ...

  5. BZOJ4360 : achievement

    对于$mode=0$的情况: 假设已经知道了最终要做哪些成就,那么这些成就一定是按$b$递减做的. 将成就按$b$从大到小排序,考虑往已选集合里新加一个成就. 假设该成就前面有$t$个已选成就,后面成 ...

  6. BZOJ1950 : [Ceoi2006]Link

    显然在最优解中,添加的边都是从$1$出发的. 这个图是一个环套树的结构,对于树的部分,显然叶子节点必须加边. 因此可以自底向上确定树中哪些节点需要加边,同时得到$1$到环上每个点的距离. 对于每个环, ...

  7. BZOJ4546(原) : 三元组

    设$f(x)=\sum_{x|d}p(d)$. 则$ans=\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\mu(i)\mu(j)\mu(k)f(lcm(i,j))f(lcm ...

  8. BZOJ3564 : [SHOI2014]信号增幅仪

    先把所有点绕原点逆时针旋转(360-a)度,再把所有点横坐标除以放大倍数p,最后用随机增量法求最小圆覆盖即可. 时间复杂度期望$O(n)$ #include<cstdio> #includ ...

  9. iframe更新与隐藏

    http://blog.sina.com.cn/s/blog_535161d80100aho6.html 从近期项目中抽取出来的一个关于iframe进行控制的代码,不是很全,不过大体功能已经显示出来了 ...

  10. POJ - Ubiquitous Religions

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰. 你知道在你的大学里有n个学生(0 < n <= 50000) .你无法询问 ...