bp123
bp123

Reputation: 3427

How do I use NumberDecimal()

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
    }
  );

Upvotes: 0

Views: 991

Answers (1)

D. SM
D. SM

Reputation: 14530

NumberDecimal exists in the mongo shell. It does not exist in nodejs (or in vanilla js).

From vanilla js you should be able to use extended json syntax:

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

Upvotes: 1

Related Questions