MongoDB and C# decimals

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).

You can read more about how to neatly handle custom type converters in my previous article.

Leave a comment