How to get numeric value from the numeric value from $numberInt

I have a object which have some key and values

{
 "mrp": {
         "$numberInt": "10000"
  },
 "dp": {
         "$numberInt": "90000"
  }
}

I want the above values like this

{
  "mrp":100000, 
  "dp":9000
}

Hi @Zubair_Rajput,

Where do you get those, and can you provide a sample code? It’s likely you’re mixing JSON and Extended JSON (that has a wider variety of data types - hence needs each value to have type specified), but the context is everything here, to understand what can be corrected.

I am reading a document then I am parsing it and mapping a new array with mrp price value
which is integer but I am getting the like below

"mrp": {
         "$numberInt": "160000"
  },
{
    "_id": "6466279bec6576a00b527434",
    "brand": "SS",
    "product_name": "Gunther ",
    "gst": 0,
    "mrp":16000
}

var product = productColl.findOne({"_id": new BSON.ObjectId("6466279bec6576a00b527434") })
        const product_item = EJSON.parse(product);
        
        var orderObj
        orderObj = order_items.map((item, idx) => {
          return {
            ...item,
            brand:  product_item.brand,
            product_name: product_item.product_name
          }
})

Sir actually I got the solution after changing

JSON.parse(EJSON.stringify(product));

to

EJSON.parse(EJSON.stringify(product));

Now I am getting like this


"product_name": "Cr. Bats",
"mrp": 100000,
"dp": 98000,

is it the correct way to do this

EJSON.parse(EJSON.stringify(product));

This is redundant, you’re converting to string, then back to object again, why don’t you access the product fields directly, as in product.brand, product.product_name, etc.?

The only point I see in your code that needs correction is that you should wait for the query to be done, because findOne() returns a Promise, as in

var product = await productColl.findOne({"_id": new BSON.ObjectId("6466279bec6576a00b527434") })
2 Likes