Monetary Data Type Storage in MongoDB
Tags: mongodb, monetaryvalues, numberdecimal
Published: 2017-11-12 15:50:53

---
title: "Monetary Data Type Storage in MongoDB"
published: true
tags: MongoDB,Monetary Values,NumberDecimal
canonical_url: https://www.kenwalger.com/blog/nosql/mongodb/monetary-data-type-storage-mongodb/
---

One piece of information that is frequently stored in databases is monetary data. Sometimes this poses challenges in data storage as we are left with decisions to be made as to what value to store.&nbsp;One option to store data is to store the data strictly in numeric value. If an item costs $12.99, we could store the monetary value of 12.99 and designate it as USD.

### Troubles with Monetary Values

This often can lead to some rounding and data precision issues however when using double values. For example, if a value is 13.4999999999 it might be stored as 13.5000000000. These rounding issues can obviously pose problems over the long run.

Another option might be to store the value as a string which, while maintaining precision, poses some challenges for doing calculations on the data. Another frequent method to store monetary data is to store the value in cents value of the item. Here in the United States, for example, we use dollars that are valued at one-hundred cents. Therefore we could store the value of a $11.99 item as 1199 and then perform conversion calculations to get back to 11.99.

### Storing Monetary Values

As mentioned above, we can store data in a variety of ways. In general, however, two basic approaches are taken.

The first approach is to store the value that is displayed to customers, $11.99 in our example as a string and then to also store an approximate value as a float. Something along the lines of:

```
{
    product_name: "fidget spinner",
    price: { value: "11.99", float_value: 11.99000000000, currency: "USD" }
}
```

That certainly works, but it still has the potential for rounding errors and there are two different values that must be updated. Wouldn’t it be great if we could simply store our&nbsp;_value_ in the database? Fortunately, there is.

###### NumberDecimal in MongoDB

One of the features in [version 3.4](http://www.kenwalger.com/blog/nosql/new-version-new-features/)&nbsp;of [MongoDB](https://www.mongodb.com) is support for the [NumberDecimal](https://docs.mongodb.com/manual/core/shell-types/#shell-type-decimal) data type. 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&nbsp;values. It is an implementation of the [BSON decimal type](https://docs.mongodb.com/manual/reference/bson-types/). Since MongoDB stores data in BSON format, it allows us to model monetary data in our database with ease.

Now, for our [fidget spinner](https://www.amazon.com/gp/product/B01N7T7FPY/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01N7T7FPY&linkCode=as2&tag=kenwalgersite-20&linkId=3f434a08033985bcdf0b41fc8a19ca92) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=B01N7T7FPY) product, we can model our data using NumberDecimal and take advantage of its features.

```
{
    product_name: "fidget spinner",
    price: { value: NumberDecimal("11.99"), currency: "USD" }
}
```

This allows us to not need a scale factor either. Monetary data will be stored on the server in a mathematically useful fashion. This allows for calculations to be made on the server using MongoDB’s [aggregation pipeline](https://docs.mongodb.com/manual/core/aggregation-pipeline/). By doing calculations on the server we get less network traffic which can ultimately lead to better applications.

### Wrap Up

I would highly recommend looking at and using NumberDecimal for your data type when storing monetary data in MongoDB. It is another reason to upgrade to version 3.4. If you haven’t upgraded yet, it might be a good time to do so. Or even better, check out their DBaaS offering [Atlas](http://www.kenwalger.com/blog/nosql/mongodb/mongodb-atlas/).

There are several MongoDB specific terms in this post. I created a [MongoDB Dictionary](https://www.echoskillstore.com/MongoDB-Dictionary/45103) skill for the [Amazon Echo](https://www.amazon.com/gp/product/B01DFKC2SO/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01DFKC2SO&linkCode=as2&tag=kenwalgersite-20&linkId=f9e513223de2525a72b95cf9561db55b)&nbsp;line of products. Check it out and you can say “Alexa, ask MongoDB what is BSON? and get a helpful response.

* * *

_Follow me on Twitter [@kenwalger](https://www.twitter.com/kenwalger) to get the latest updates on my postings._

[![Facebook](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png?resize=48%2C48&ssl=1 "Share on Facebook")](http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F&t=Monetary%20Data%20Type%20Storage%20in%20MongoDB&s=100&p%5Burl%5D=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F&p%5Bimages%5D%5B0%5D=https%3A%2F%2Fi2.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F07%2Fmonetary-1-e1500578574705.png%3Ffit%3D125%252C125%26ssl%3D1&p%5Btitle%5D=Monetary%20Data%20Type%20Storage%20in%20MongoDB "Share on Facebook")[![twitter](https://i0.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png?resize=48%2C48&ssl=1 "Share on Twitter")](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F&text=Hey%20check%20this%20out "Share on Twitter")[![google_plus](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png?resize=48%2C48&ssl=1 "Share on Google+")](https://plus.google.com/share?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F "Share on Google+")[![reddit](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png?resize=48%2C48&ssl=1 "Share on Reddit")](http://www.reddit.com/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F&title=Monetary%20Data%20Type%20Storage%20in%20MongoDB "Share on Reddit")[![linkedin](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png?resize=48%2C48&ssl=1 "Share on Linkedin")](http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F&title=Monetary%20Data%20Type%20Storage%20in%20MongoDB "Share on Linkedin")[![mail](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png?resize=48%2C48&ssl=1 "Share by email")](mailto:?subject=Monetary%20Data%20Type%20Storage%20in%20MongoDB&body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fmonetary-data-type-storage-mongodb%2F "Share by email")

The post [Monetary Data Type Storage in MongoDB](https://www.kenwalger.com/blog/nosql/mongodb/monetary-data-type-storage-mongodb/) appeared first on [Blog of Ken W. Alger](https://www.kenwalger.com/blog).