Need to store high precision decimal values ​​in MongoDB

I have little experience with MongoDB. I usually work on large SQL server databases.

MongoDB only supports double and no decimal . The C # driver serializes decimal numbers as strings.

  • What functionality will I skip when storing decimal numbers as strings in MongoDB?

  • Is there a way to set the default decimal serialization to double (AllowTruncation) without having to add an attribute to each property?

  • What do I lose in accuracy if I used a Bson double?

Thank you for your help!

UPDATE

I have an existing application model that uses decimal numbers in C #. I want to use MongoDB as a new level of database and change as little as possible in an existing application. That's why I'm looking for a way to match decimal numbers in C # to double in MongoDB.
I understand that I am losing accuracy and should analyze side effects. My only remaining question is to find out if there is a way to set default decimal serialization as double. Thanks again. Great answers and comments so far.

+7
decimal c # mongodb
source share
2 answers

I will partially answer your question (because I know little about C #).

So, what do you lose if you save the decimal numbers as strings.

  • your numbers will weigh more on average (each double number costs 8 bytes for storage , which means that each line with more than 8 characters will be more weight). Because of this, your indexes (if they are built on this field, will grow)
  • you cannot use operators that take numbers as arguments $ inc , $ bit , $ mod , $ min , $ max and in version 2.6 of $ mul . (Maybe I forgot something)
  • you cannot compare numbers (maybe "1.65" and "1.23" is comparable to a string, but definitely not numbers with e and minuses somewhere in the middle). Due to these operations, which are based on comparison, for example $ sort , and all these $ gte, $ gt, $ lte, $ lt will not work correctly.

What do you lose in accuracy if you save the decimal as double :

  • based on this, Decimal in C # has 28-29 significant digits, while looking at my first link and checking the spec for double precision , you see that it has 15-17 significant digits. This is basically what you lose.
  • The following really important thing that people sometimes forget when working with double floats:

.

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 2nd subquery:

Is there a way to set default serialization of decimal places to double (AllowTruncation) without having to assign an attribute to each property?

I really do not understand this, so I hope someone can answer it.

+7
source share

According to Mongodb 3.4 and 2.4, Mongodb C # driver supports decimal types.

Your document’s properties should have the [BsonRepresentation(BsonType.Decimal128)] attribute found in the MongoDB.Bson.Serialization.Attributes namespace.

this will display on "YourDecimalValue" : NumberDecimal("100.0000") in MongodDB. Robomongo supports the new decimal type from version 1.1 Beta.

+3
source share

All Articles