需要高精度的十进制值存储在MongoDB中 [英] Need to store high precision decimal values in MongoDB

查看:203
本文介绍了需要高精度的十进制值存储在MongoDB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MongoDB的一点经验。我平时在大尺度上的SQL Server DB的工作。



MongoDB中只支持双和无小数点。 C#的驱动程序序列化小数为字符串。




  • 我错过了什么功能,如果我存储小数位在$ B $字符串b MongoDB的?


  • 有没有一种方法来设置小数位的双
    (AllowTruncation)的默认的序列化,而不必把一个属性上的每个
    属性?


  • 我怎么失去精度,如果我使用BSON双?




感谢您的帮助!



更新



我有一个现有的应用程序使用小数在C#中的模型。我想使用的MongoDB作为一个新的DB层,在现有的应用程序,从而使我们少。为什么我在寻找一种方法那映射的小数的在C#中的的MongoDB中。

我知道我失去精度,就必须分析它的副作用。我唯一剩下的问题是要知道是否有设置小数双倍的默认序列化的方式。搜索结果
再次感谢。伟大的答案和意见为止。


解决方案

我将部分地回答你的问题(因为我不知道很多关于C#)。



那么你会失去,如果你将小数存储为字符串。




  • 您的平均体重会更数(每双号花费8个字节来存储这意味着每个字符串有更多的则8个字符将体重更多)。由于这些索引时(如果他们将建立在这一领域将增长)

  • 你将无法使用运营商这需要数字作为参数的$inc ,的 $位 $ MOD $分钟,的$max 和2.6版本的$mul 。 (可能是我忘了的东西)

  • 您将无法比较数字(可能是1.65和1.23相媲美作为一个字符串,但绝对不是随e和短处号介于两者之间)。由于这种操作,这些操作建立在像 $比较顶级的排序,而所有这些<一的HREF =http://docs.mongodb.org/manual/reference/operator/query/gt/> $ GTE,GT $,$ LTE,$ LT 将无法正常工作。



你将在精密失去,如果你存储小数为双




  • 根据此,小数在C#有28- 29显著数字,而看着我的第一个环节,并检查双精度规范你看,它有15-17显著数字。这基本上就是你将失去

  • 人们用双浮筒打交道时,有时候忘了另外一个非常重要的事情如下图所示:

$ B $ ; b

  db.c.insert({_ ID:1,b:3.44})
db.c.update({_ ID:1},{$ INC:{b:1}})
db.c.find({b:4.44})//跆拳道,哪里是我的文档?那里没有什么



关于2-ND subquestion:




有没有一种方法来设置小数位的双
(AllowTruncation)的默认的序列化,而不必把一个属性上的每个属性<? / p>


我并不真正了解它,所以我希望有人能回答这个问题。


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!

UPDATE

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.

解决方案

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.

  • your numbers on average would weight more (each double number cost 8 bytes to store which means that every string that has more then 8 chars will weight more). Because of these your indexes (if they will be built on this field would grow)
  • you will not be able to use operators which takes numbers as arguments $inc, $bit, $mod, $min, $max and in 2.6 version $mul. (May be I forgot something)
  • you will not be able to compare numbers (may be '1.65' and '1.23' is comparable as a string, but definitely not numbers with e and minuses somewhere in between). Because of this operations which build on top of comparison like $sort, and all these $gte, $gt, $lte, $lt will not work correctly.

What will you lose in precision if you store decimal as double:

  • based on this, Decimal in C# has 28-29 significant digits, whereas 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 will lose
  • another really important thing which people sometimes forget when dealing with double floats is illustrated below:

.

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.

这篇关于需要高精度的十进制值存储在MongoDB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆