{"id":465,"date":"2017-07-20T12:28:58","date_gmt":"2017-07-20T19:28:58","guid":{"rendered":"http:\/\/www.kenwalger.com\/blog\/?p=465"},"modified":"2017-07-20T12:28:58","modified_gmt":"2017-07-20T19:28:58","slug":"monetary-data-type-storage-mongodb","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/monetary-data-type-storage-mongodb\/","title":{"rendered":"Monetary Data Type Storage in MongoDB"},"content":{"rendered":"<p>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.\u00a0One 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.<\/p>\n<h3>Troubles with Monetary Values<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>Storing Monetary Values<\/h3>\n<p>As mentioned above, we can store data in a variety of ways. In general, however, two basic approaches are taken.<\/p>\n<p>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:<\/p>\n<pre>{\n    product_name: \"fidget spinner\",\n    price: { value: \"11.99\", float_value: 11.99000000000, currency: \"USD\" }\n}\n<\/pre>\n<p>That certainly works, but it still has the potential for rounding errors and there are two different values that must be updated. Wouldn&#8217;t it be great if we could simply store our\u00a0<em>value<\/em> in the database? Fortunately, there is.<\/p>\n<h6>NumberDecimal in MongoDB<\/h6>\n<p>One of the features in <a href=\"http:\/\/www.kenwalger.com\/blog\/nosql\/new-version-new-features\/\">version 3.4<\/a>\u00a0of <a href=\"https:\/\/www.mongodb.com\">MongoDB<\/a> is support for the <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/shell-types\/#shell-type-decimal\">NumberDecimal<\/a> 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\u00a0values. It is an implementation of the <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/bson-types\/\">BSON decimal type<\/a>. Since MongoDB stores data in BSON format, it allows us to model monetary data in our database with ease.<\/p>\n<p>Now, for our <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01N7T7FPY\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01N7T7FPY&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=3f434a08033985bcdf0b41fc8a19ca92\" target=\"_blank\" rel=\"noopener\">fidget spinner<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B01N7T7FPY\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> product, we can model our data using NumberDecimal and take advantage of its features.<\/p>\n<pre>{\n    product_name: \"fidget spinner\",\n    price: { value: NumberDecimal(\"11.99\"), currency: \"USD\" }\n}\n<\/pre>\n<p>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&#8217;s <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/aggregation-pipeline\/\">aggregation pipeline<\/a>. By doing calculations on the server we get less network traffic which can ultimately lead to better applications.<\/p>\n<h3>Wrap Up<\/h3>\n<p>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&#8217;t upgraded yet, it might be a good time to do so. Or even better, check out their DBaaS offering <a href=\"http:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/mongodb-atlas\/\">Atlas<\/a>.<\/p>\n<p>There are several MongoDB specific terms in this post. I created a <a href=\"https:\/\/www.echoskillstore.com\/MongoDB-Dictionary\/45103\">MongoDB Dictionary<\/a> skill for the <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01DFKC2SO\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01DFKC2SO&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=f9e513223de2525a72b95cf9561db55b\" rel=\"noopener noreferrer\">Amazon Echo<\/a>\u00a0line of products. Check it out and you can say &#8220;Alexa, ask MongoDB what is BSON?&#8221; and get a helpful response.<\/p>\n<hr \/>\n<p><em>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\">@kenwalger<\/a> to get the latest updates on my postings.<\/em><\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F465&#038;t=Monetary%20Data%20Type%20Storage%20in%20MongoDB&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F465&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F07%2Fmonetary-1-e1500578574705.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=Monetary%20Data%20Type%20Storage%20in%20MongoDB\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F465&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"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&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F465&#038;title=Monetary%20Data%20Type%20Storage%20in%20MongoDB\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F465&#038;title=Monetary%20Data%20Type%20Storage%20in%20MongoDB\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Monetary%20Data%20Type%20Storage%20in%20MongoDB&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F465\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>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.\u00a0One option to store data is to store the data strictly in numeric value. If an item costs $12.99, we could &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/monetary-data-type-storage-mongodb\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Monetary Data Type Storage in MongoDB&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":471,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[664,663],"yst_prominent_words":[268,624,99,628,657,615,620,627,87,626,715,631,629,622,659,1048,658,614,625,334],"class_list":["post-465","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-monetary-values","tag-numberdecimal","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/monetary-1-e1500578574705.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-7v","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=465"}],"version-history":[{"count":2,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/465\/revisions"}],"predecessor-version":[{"id":468,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/465\/revisions\/468"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/471"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=465"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}