Storing Enums as strings in MongoDB
Is there a way to store Enums as string names rather than ordinal values?
Example:
Imagine I've got this enum:
public enum Gender
{
Female,
Male
}
Now if some imaginary User exists with
...
Gender gender = Gender.Male;
...
it'll be stored in MongoDb database as { ... "Gender" : 1 ... }
but i'd prefer something like this { ... "Gender" : "Male" ... }
Is this possible? Custom mapping, reflection tricks, whatever.
My context: I use strongly typed collections over POCO (well, I mark ARs and use polymorphism occasionally). I've got a thin data access abstraction layer in a form of Unit Of Work. So I'm not serializing/deserializing each object but I can (and do) define some ClassMaps. I use official MongoDb driver + fluent-mongodb.
Solution 1:
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class Person
{
[JsonConverter(typeof(StringEnumConverter))] // JSON.Net
[BsonRepresentation(BsonType.String)] // Mongo
public Gender Gender { get; set; }
}
Solution 2:
The MongoDB .NET Driver lets you apply conventions to determine how certain mappings between CLR types and database elements are handled.
If you want this to apply to all your enums, you only have to set up conventions once per AppDomain (usually when starting your application), as opposed to adding attributes to all your types or manually map every type:
// Set up MongoDB conventions
var pack = new ConventionPack
{
new EnumRepresentationConvention(BsonType.String)
};
ConventionRegistry.Register("EnumStringConvention", pack, t => true);
Solution 3:
You can customize the class map for the class that contains the enum and specify that the member be represented by a string. This will handle both the serialization and deserialization of the enum.
if (!MongoDB.Bson.Serialization.BsonClassMap.IsClassMapRegistered(typeof(Person)))
{
MongoDB.Bson.Serialization.BsonClassMap.RegisterClassMap<Person>(cm =>
{
cm.AutoMap();
cm.GetMemberMap(c => c.Gender).SetRepresentation(BsonType.String);
});
}
I am still looking for a way to specify that enums be globally represented as strings, but this is the method that I am currently using.
Solution 4:
With driver 2.x I solved using a specific serializer:
BsonClassMap.RegisterClassMap<Person>(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.Gender).SetSerializer(new EnumSerializer<Gender>(BsonType.String));
});
Solution 5:
Use MemberSerializationOptionsConvention to define a convention on how an enum will be saved.
new MemberSerializationOptionsConvention(typeof(Gender), new RepresentationSerializationOptions(BsonType.String))