Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

How do I use NumberDecimal() within Node.js?

I’ve posted this on StackOverflow as well.

I’m trying to store currency data in MongoDB using NumberDecimal() as per the documentation suggests, however, I’m getting ReferenceError: NumberDecimal is not defined . I’m running db version v4.2.2 and MongoDB shell version v4.2.2 within Nodejs.

What am I missing here? Is this an import problem.

const result = await client
  .db('PurchaseOrders')
  .collection('purchaseOrders')
  .updateOne(
    { _id: ObjectId(_id) },
    {
      $set: { data: NumberDecimal('1.02') }
    },
    {
      upsert: true
    }
  );

It is defined as Decimal128 in NodeJS MongoDB driver.

The static method Decimal128.fromString(string) can be used to create a Decimal128 instance from a string.

1 Like