Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: Is it possible to set the case of a BsonElement by default?

I am using the Mongodb official driver and I want to set by default the naming of the elements to lower case to avoid code like this:

public class Localization
{
    [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
    public int Id { get; set; }
    [BsonRequired]
    [BsonElement("country")]
    public string Country { get; set; }

In this sample I want that by default the element name be "country" not "Country" aka lower case. Is it possible?

Thanks

like image 381
David Graça Avatar asked Dec 01 '22 21:12

David Graça


2 Answers

A small update since the BsonClassMap.RegisterConventions is marked as obsolete

var camelCaseConventionPack = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConventionPack, type => true);
like image 54
Tim Skauge Avatar answered Dec 23 '22 16:12

Tim Skauge


var conventions = new ConventionProfile();
conventions.SetElementNameConvention(new CamelCaseElementNameConvention());

BsonClassMap.RegisterConventions(conventions, t => true);

MongoDB CSharp Driver Serialization Tutorial

like image 21
Jordan Neill Avatar answered Dec 23 '22 15:12

Jordan Neill