Reputation: 10463
I have little experience with MongoDB. I am usual working on large scale SQL server DBs.
MongoDB only supports double and there is no decimal. The C# driver serializes decimals as strings.
What functionality do I miss if I store decimals as strings in MongoDB?
Is there a way to set a default serialization of decimals as double (AllowTruncation) without having to put an Attribute on each property?
What do I lose in precision if I used Bson double?
Thanks for your help!
I have an existing application model that uses decimals in C#. I want to use MongoDB as a new DB layer and change as little in the existing app as possible. Thats why I am looking for a way to map decimals in C# to double in MongoDB.
I understand that I loose precision and would have to analyze the side effects of it. My only remaining question is to know if there is a way to set a default serialization of decimals as double.
Thanks again. Great answers and comments so far.
Upvotes: 8
Views: 7390
Reputation: 2112
As of Mongodb 3.4 and the 2.4 Mongodb C# driver, decimal types are supported.
The properties of your document must have the [BsonRepresentation(BsonType.Decimal128)]
attribute found in the MongoDB.Bson.Serialization.Attributes
namespace.
this will map to "YourDecimalValue" : NumberDecimal("100.0000")
in MongodDB. Robomongo supports the new decimal type from version 1.1 Beta.
Upvotes: 8
Reputation: 222591
I will answer your question partially (because I do not know much about C#).
So what will you lose if you will store decimals as strings.
What will you lose in precision if you store decimal as double:
.
db.c.insert({_id : 1, b : 3.44})
db.c.update({_id : 1},{$inc : {b : 1}})
db.c.find({b: 4.44}) // WTf, where is my document? There is nothing there
Regarding the 2-nd subquestion:
Is there a way to set a default serialization of decimals as double (AllowTruncation) without having to put an Attribute on each property?
I do not really understood it, so I hope someone would be able to answer it.
Upvotes: 8