Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# Driver does not automap pascal-cased properties

I'm using the official MongoDB C# driver to query an existing collection of documents that use camel-cased property names e.g. post.title.

The docs seem to suggest that nothing is required to map the documents to C# POCOs with pascal-cased property names e.g:

public class Post
{
    public ObjectId Id { get; set; }
    public string Title { get; set; }
    public string[] Tags { get; set; }
}

However, when attempting to query the collection I get the exception:

Element 'title' does not match any field or property of class MongoDBDemo.Post.

If change Post.Title to Post.title the query executed successfully.

Is there a way to tell the driver to deserialize to pascal-cased properties and serialize to camel-cased properties by default?

like image 627
Ben Foster Avatar asked Mar 22 '14 09:03

Ben Foster


People also ask

What is Mongo-C in C?

MongoDB C-driver, formerly known as mongo-c, is a project comprising two libraries: libmongoc, a client library that has been written in C for MongoDB. libbson, a library that offers practical routines related to building, parsing, and iterating BSON documents, the native data format of MongoDB.

What is the MongoDB C driver?

The MongoDB C Driver, also known as "libmongoc", is the official client library for C applications, and provides a base for MongoDB drivers in higher-level languages. The library is compatible with all major platforms.

What is libmongoc in MongoDB?

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 MongoDB used for?

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. No database makes you more productive. Try MongoDB free in the cloud! As a programmer, you think in objects. Now your database does too. MongoDB is a document database, which means it stores data in JSON-like documents.


2 Answers

I know this is pretty old, but I found that the following can be used to map the camelCased fields in documents to PascalCased properties in C# POCOs:

var conventionPack = new ConventionPack
{
    new CamelCaseElementNameConvention()
};

ConventionRegistry.Register(
    name: "CustomConventionPack", 
    conventions: conventionPack,
    filter: t => true);

For filter I am using t => true so that this camelCase convention applies to all my entities/collections.

Advantage of this approach over using BsonElement attribute is that this approach can be used in cases when entities/POCOs do not reside in the same project as the one having mongodb driver reference in it (e.g. in repository pattern, or in the clean architecture).

Read more from the official documentation here.

like image 158
Sнаđошƒаӽ Avatar answered Sep 21 '22 05:09

Sнаđошƒаӽ


The docs actually say the opposite:

Normally the name of the field in the database is exactly the same as the name of the field or property in your domain class, but Id is an exception and is mapped to _id in the database.

You could investigate creating your own Convention to override the names and make a Pascal based naming scheme. You could have it be automatically applied to every class you use with MongoDb for example.

Or, you could manually specify new names either through attributes:

public class Post {
    [BsonElement("title")]
    public string Title { get; set; }
}

Or, the class map:

BsonClassMap.RegisterClassMap<Post>(cm => {
    cm.AutoMap();
    cm.GetMemberMap(c => c.Title).SetElementName("title");
});
like image 43
WiredPrairie Avatar answered Sep 18 '22 05:09

WiredPrairie