Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

money representation in mongoid

How should I work with Money with MongoID? Should I configure it as BigDecimal? And at rails level? For ActiveRecord we have something called Money, but AFAIK it just supports AR

like image 671
VP. Avatar asked Sep 28 '10 15:09

VP.


People also ask

What data type is currency in MongoDB?

NumberDecimal in MongoDB This data type allows for 128-bit decimal based values. It is specifically designed and intended for use for applications needing to store monetary or high precision values. It is an implementation of the BSON decimal type.

How does MongoDB store prices?

The proper type to use for MongoDB is the Decimal128 type, since it stores exact representations of the values, and it has enough precision to support all forms of monetary calculation. Show activity on this post. You can store the Double BSON type to store the price value.

How do you round off in MongoDB?

For example, $round : [1234.5678, 2] rounds to two decimal places and returns 1234.57 . If <place> resolves to a negative integer, $round rounds using the digit <place> to the left of the decimal. For example, $round : [1234.5678, -2] uses the 2nd digit to the left of the decimal ( 3 ) and returns 1200 .

What is double MongoDB?

Double: The double data type is used to store the floating-point values. Example: In the following example we are storing the marks of the student in the student collection: 4. Boolean: The boolean data type is used to store either true or false.


1 Answers

I ran into this also. Unfortunately BigDecimal stores in Mongodb as a string, so it won't let you sum, sort, etc on it like a float or int.

Integer seem to be the way to go storing the value in cents, possibly using the Money gem to abstract it a bit: https://github.com/RubyMoney/money

Mongo stores the int using 64 bits on most modern machines I think so there is not much risk of needing a larger amount even in cents. It looks like you can store between −9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 cents, so take off two decimal places to get your min/max value in dollars.

http://en.wikipedia.org/wiki/Integer_(computer_science)

like image 77
Brian Armstrong Avatar answered Sep 19 '22 19:09

Brian Armstrong