16

I'm using v0.9 of the official MongoDB driver and i'm trying to read in a collection. I have a field in the database that I don't want to read into my object but I get the following error.

"Unexpected element: Network"

The collection looks like this in the database

Merchants
 - _id
 - Name
 - Description
 - Url
 - Network

When I read it into C# I want to create an object called Merchant that has all of the same properties, except "Network". How do I do this?

Alex
  • 34,776
  • 10
  • 53
  • 68

1 Answers1

26

There's an "IgnoreExtraElements" option on the BSON serializer which you can enable to prevent that error.

Either set it as an attribute on your Merchant class:

[BsonIgnoreExtraElements]
public Merchant {
    // fields and properties
}

or in code if you're using class maps:

BsonClassMap.RegisterClassMap<Merchant>(cm => {
    cm.AutoMap();
    cm.SetIgnoreExtraElements(true);
});
Martin Owen
  • 5,221
  • 3
  • 35
  • 31
  • 2
    Is there a way to ignore extra elements on all entities without having to specify it for each entity separately? – Jos Sep 17 '14 at 12:36
  • 2
    I found the solution for my own question in stackoverflow: http://stackoverflow.com/questions/12944520/implement-for-all-classes-bsonignoreextraelements – Jos Sep 17 '14 at 12:38