<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kn8</title>
    <description>The latest articles on DEV Community by Kn8 (@kn8a).</description>
    <link>https://dev.to/kn8a</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F930674%2F2da2728b-6ea9-4500-9f04-c2ece32b9142.png</url>
      <title>DEV Community: Kn8</title>
      <link>https://dev.to/kn8a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kn8a"/>
    <language>en</language>
    <item>
      <title>decimal128 vs double in MongoDB</title>
      <dc:creator>Kn8</dc:creator>
      <pubDate>Fri, 13 Jan 2023 10:09:47 +0000</pubDate>
      <link>https://dev.to/kn8a/decimal128-vs-double-in-mongodb-4oe4</link>
      <guid>https://dev.to/kn8a/decimal128-vs-double-in-mongodb-4oe4</guid>
      <description>&lt;p&gt;When it comes to storing numbers in MongoDB, you've got two options: Decimal128 and Double. Think of Decimal128 like a fancy calculator with 34 decimal places of precision, perfect for financial and scientific calculations. Double, on the other hand, is like a regular calculator that's great for storing large range of numbers and doesn't require as much precision.&lt;/p&gt;

&lt;p&gt;Let's say you're building a stock trading app. You'll want to use Decimal128 for storing the exact price of a stock to ensure accuracy for your users. But for storing the temperature in Celsius in a weather application, Double will be a better option as it's more efficient and doesn't require that level of precision.&lt;/p&gt;

&lt;p&gt;Keep in mind, Decimal128 is not a native MongoDB data type, so you'll need to use a library like 'mongodb-decimal' to work with it. Double, however, is native to MongoDB, so no additional libraries are needed.&lt;/p&gt;

&lt;p&gt;In short, Decimal128 is like a fancy calculator for precise calculations and Double is like a regular calculator for storing large range of numbers with less precision. Choose the one that best fits your needs and have fun!&lt;/p&gt;

&lt;p&gt;Here are a few examples of how to use the Decimal128 data type in MongoDB:&lt;/p&gt;

&lt;p&gt;Using the mongodb-decimal library:&lt;br&gt;
You can use the mongodb-decimal library to work with Decimal128 data type in MongoDB. First, you need to install the library by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install mongodb-decimal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can use it in your code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Decimal128 = require('mongodb-decimal');
const decimalValue = new Decimal128("123.45");
// insert into a collection
await collection.insertOne({ value: decimalValue });

// find and update
await collection.updateOne({}, { $set: { value: new Decimal128("999.99") } });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the Decimal128 constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const decimalValue = new Decimal128("123.45");

// insert into a collection
await collection.insertOne({ value: decimalValue });

// find and update
await collection.updateOne({}, { $set: { value: new Decimal128("999.99") } });
Using the Decimal128.fromString() method:
Copy code
const decimalValue = Decimal128.fromString("123.45");

// insert into a collection
await collection.insertOne({ value: decimalValue });

// find and update
await collection.updateOne({}, { $set: { value: Decimal128.fromString("999.99") } });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In all the examples above, the &lt;code&gt;decimalValue&lt;/code&gt; variable holds the Decimal128 value, which can be inserted or updated into a MongoDB collection.&lt;/p&gt;

&lt;p&gt;Note that these are just examples and you should make sure to handle any errors and also use appropriate MongoDB driver version to use Decimal128.&lt;/p&gt;

</description>
      <category>startup</category>
      <category>career</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
