MongoDB used to serialize C# decimals to strings. This wasn't an issue when simply storing and retrieving values. However, if you'd like to search or sort by the value, it makes the value pretty useless. The decimal value would need to be stored both as a decimal (precise number) for use and a double for MongoDB operations which is only an approximation.
Starting with MongoDB 3.4, we can add a value type converter (or serializer).
1 2 |
BsonSerializer.RegisterSerializer(typeof(decimal), new DecimalSerializer(BsonType.Decimal128)); BsonSerializer.RegisterSerializer(typeof(decimal?), new NullableSerializer<decimal>(DecimalSerializer(BsonType.Decimal128))); |
You can read more about how to neatly handle custom type converters in my previous article.