在MongoDB中提升BigDecimal商店 [英] Lift store BigDecimal in MongoDB

查看:289
本文介绍了在MongoDB中提升BigDecimal商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB在Lift中开发一个电子商务网站

I am developing an ecommerce site in Lift with MongoDB

我需要存储一些产品价格数据

I need to store some data for product prices

我的问题是这个

如果要存储BigDecimal数据类型,应该在MongoDB中使用哪种字段类型

What field type should I use in MongoDB if I want to store data type BigDecimal

在mapper中是一个类型为MappedDecimal的字段,但是

In mapper is a field of type: MappedDecimal, but

在net.liftweb.mongodb.record.field中没有等效内容

in net.liftweb.mongodb.record.field no equivalent

如何存储此信息?

感谢大家的关注和帮助

推荐答案

从MongoDB v2.6开始,没有固定位数的十进制类型.数据必须保存在不同类型的字段中,并且应用程序每次都必须执行翻译.

As of MongoDB v2.6 there is no fixed-places decimal type. The data has to be saved in a field of a different type and the application must perform translation everytime.

可能,中间库可以执行此翻译,而不是您的应用程序.我猜net.liftweb.record没有.

Potentially the intermediary libs could do this translation instead of your application. I guess net.liftweb.record does not.

如果双精度类型足以满足所讨论的字段的要求,我建议将其更改为简单起见.但是,假设您出于充分的理由使用BigDecimal,则有一些众所周知的解决方法.这些是:

If a double type would suffice for the field(s) in question, I recommend changing to that for simplicity. But assuming you are using BigDecimal for good reasons, there are well-known workarounds. These are:

(1)将其存储为字符串.您可以具有任意精度.但是,仅当您每次在左侧用零填充固定长度时,才可以对精确值匹配进行排序或查询.即使这样,正数和负数也是两个不同的排序范围.负数需要反向排序以进行正确的数字排序. MongoDB命令的示例自然会返回这些零填充的字符串数字:

(1) Store it as a string. You can have any arbitrary precision. But sorting or querying for exact value matches will only work if you pad the left side with zeroes to a fixed length every time. Even then positive and negative numbers are two different ranges sorting-wise. The negatives need to be sorted in reverse to have correct numerical sorting. An example of the order MongoDB will naturally return these zero-padded string numbers:

"-0000054321.9876"
"-0000100322"
"0000054321.9876"
"0000100322"

我相信BigDecimal类型具有从字符串值构造的构造函数,因此这可能是在应用程序的转换函数中最容易实现的

I believe the BigDecimal type has a constructor from a string value, so this might be the easiest to implement in your application's translation function.

(2)将其存储为移位的长整数(Int64).排序有效,使用较少的磁盘空间,负负数没有问题.积极的.需要将值上移固定倍数,这在直接查看数据库时有点难以理解.对于整个集合中的所有值,必须将精度固定为相同-对于财务用例来说是可以的;对于某些科学用例而言,效果不佳.

(2) Store it as a shifted long (Int64). Sorting works, less disk space is used, no problems with negative v.s. positive. Requires shifting the values up by a fixed multiple which makes a bit unreadable when looking at the database directly. The precision has to be fixed to be the same for all the values in the whole collection- OK for financial use cases; not OK for some scientific use cases.

(3)存储为一对数字,一个存储在小数点的两侧.排序需要额外的工作.如果使用Int32数字,则精度将限制为小数点两侧的9位数字.当然,查看数据库中的两列而不是一列会很麻烦.

(3) Store as a pair of numbers, one for either side of the decimal point. Sorting requires a extra little work. If using Int32 numbers the precision will be limited to 9 digits either side of the decimal. Looking at two columns in the db instead of one is a little more work of course.

对于一个Scala代码示例,我发现MongoDB项目的Reactive驱动程序记录了

For a Scala code example I found the Reactive driver for MongoDB project has documented three serialization workarounds for BigDecimal. The first uses double; the latter two take yet another approach- create a whole sub-document for BigDecimal value. Trying to query values wrapped in sub-documents would be tricky I suspect.

另一个来自Ebay开发团队博客(Morphia/Java)的真实案例

P.S.也许MongoDB将来会添加一个十进制类型.您可以观看/支持公开功能请求- https://jira.mongodb. org/browse/SERVER-1393

P.S. maybe MongoDB will add a decimal type in the future. There is a open feature request for it that you can watch/upvote - https://jira.mongodb.org/browse/SERVER-1393

这篇关于在MongoDB中提升BigDecimal商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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