Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo C# ignore property

Tags:

c#

mongodb

driver

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?

like image 358
Alex Avatar asked Dec 08 '10 14:12

Alex


People also ask

What is Mongo C?

The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.

What is a Mongo driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.

How do I download MongoDB drivers?

Open-Source MongoDb JDBC DriverThe driver binaries can be downloaded as zip file, which you should uncompress. Download the zip, unpack and include the jar files in your classpath. The driver is compatible with Java 11. for any issues with the driver, you can write to us.


1 Answers

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);
});
like image 124
Martin Owen Avatar answered Oct 18 '22 03:10

Martin Owen