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. MFC MessageBox AfxMessageBox

    MessageBox 一.消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合.1.MessageBox("这是一个最简单的消息框!");2.M ...

  2. [转]走向视网膜(Retina)的Web时代

    转载出处:http://www.w3cplus.com/css/towards-retina-web.html 维基百科将Retina译为“视网膜”."Retina"一词,原意是“ ...

  3. HDU3870 Catch the Theves(平面图最小割转最短路)

    题目大概说给一个n×n的方格,边有权值,问从求(1,1)到(n,n)的最小割. 点达到了160000个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...

  4. 每天一个linux命令--awk

    统计计算日志 pmail@app2linux04 performance]$ grep 'user:logBehaviorAction' performance.log|awk -F '|' '{pr ...

  5. Azkaban 2.5.0 搭建

    一.前言 最近试着参照官方文档搭建 Azkaban,发现文档很多地方有坑,所以在此记录一下. 二.环境及软件 安装环境: 系统环境: ubuntu-12.04.2-server-amd64 安装目录: ...

  6. 解决iPhone中overflow:scroll;滑动速度慢或者卡的问题

    在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...

  7. POJ 1845 (约数和+二分等比数列求和)

    题目链接: http://poj.org/problem?id=1845 题目大意:A^B的所有约数和,mod 9901. 解题思路: ①整数唯一分解定理: 一个整数A一定能被分成:A=(P1^K1) ...

  8. ACM Registration system

    Registration system 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 A new e-mail service "Berlandesk&q ...

  9. SRM 615 DIV1 500

    TC 都615了...时间过的真快啊. 第一次做出500分,心情还是很激动的,虽然看了很久的题解,TC官网上的题解,很详细,但是英语的...我搜了搜,发现一份日语的...好吧,我还是看看英语的吧... ...

  10. 【noiOJ】p1794

    t1794:集合加法 查看 提交 统计 提问 总时间限制:  3000ms 内存限制:  65536kB 描述 给出2个正整数集合A = {pi | 1 <= i <= a},B = {q ...