Alex
Alex

Reputation: 36596

Mongo C# ignore property

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?

Upvotes: 16

Views: 10147

Answers (1)

Martin Owen
Martin Owen

Reputation: 5271

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);
});

Upvotes: 26

Related Questions