title: Extract Decimal from Decimal128 with Mongoose - MongoDB
body: <p>I'm querying Mongo in Nodejs with Mongoose and attempting to extract the numeric value of multiple fields stored as a Decimal128. However, the value is oddly wrapped in query results and I'm not sure how to extract it through Mongo or Mongoose:</p>
  
  <pre><code>{data:[
    {
    "date": {
            "$numberDecimal": "1530057600000"
    },
    "open": {
            "$numberDecimal": "86.13"
    },
    "high": {
            "$numberDecimal": "86.63"
    },
    "low": {
            "$numberDecimal": "85.47"
    },
    "close": {
            "$numberDecimal": "85.64"
    },
    "volume": {
            "$numberDecimal": "308508"
    }
  },
  </code></pre>
  
  <p>Is there a way I can use Mongo or Mongoose to convert the above JSON query-result into what's below?</p>
  
  <pre><code>{data:[
   {
    "date": 1530057600000
    "open": 86.13
    "high": 86.63
    "low": 85.47
    "close": 85.64
    "volume": 308508
   },
  </code></pre>
  
  <p>I tried selecting the fields as follows ,but this didn't work.</p>
  
  <pre><code>    data[i].date.$numberDecimal, 
      data[i].open.$numberDecimal,
      data[i].high.$numberDecimal,
      data[i].low.$numberDecimal, 
      data[i].close.$numberDecimal 
  </code></pre>
  
  <p>Here's my Mongoose schema:</p>
  
  <p><strong>Folder - Model - Stock.js</strong></p>
  
  <pre><code>const mongoose = require('mongoose')
  mongoose.Promise = global.Promise
  
  const childSchemaData = new mongoose.Schema({
    "_id": false,
    date: {type: mongoose.Types.Decimal128},
    open: {type: mongoose.Types.Decimal128},
    high: {type: mongoose.Types.Decimal128},
    low: {type: mongoose.Types.Decimal128},
    close: {type: mongoose.Types.Decimal128},
    volume: {type: mongoose.Types.Decimal128}
  })
  
  const parentSchemaSymbol = new mongoose.Schema({
    "_id": false,
    symbol: {
      type: String,
      trim: true,
      minlength: 2,
      maxlength: 4,
      uppercase: true,
      required: 'Plese enter a valid symbol, min 2 characters and max 4'
    },
    // Array of subdocuments
    data: [childSchemaData],
    slug: String
  
  })
  
  module.exports = mongoose.model('Stock', parentSchemaSymbol)
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>const mongoose = require('mongoose')
  const parentSchemaSymbol = mongoose.model('Stock')
  
  exports.dbFetch = (req, res) =&gt; {
    let curValueDbFetch = req.params.symbol
  
    const query = { symbol: `${curValueDbFetch}` }
    const projection = { _id: 0, data: 1 }
  
    parentSchemaSymbol.findOne(query, projection).then(doc =&gt; {
      return res.send(doc)
    }).catch(e =&gt; {
      console.log(e)
    })
  }
  </code></pre>
  
  <p>I am sending the data to the front end and this is what I  am receiving in the browser:</p>
  
  <p><a href="https://i.stack.imgur.com/9ZYk3.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/9ZYk3.jpg" alt="enter image description here"></a></p>
  
  <p><strong>SOLUTION</strong></p>
  
  <pre><code>const mongoose = require('mongoose')
  const parentSchemaSymbol = mongoose.model('Stock')
  
  exports.dbFetch = (req, res) =&gt; {
    let curValueDbFetch = req.params.symbol
  
    const query = { symbol: `${curValueDbFetch}` }
    const projection = { _id: 0, data: 1 }
  
    parentSchemaSymbol.findOne(query, projection).sort({ date: -1 }).then(doc =&gt; {
      let chartData = doc.data.map(item =&gt; {
        return {
          date: parseFloat(item.date), // the date
          open: parseFloat(item.open), // open
          high: parseFloat(item.high), // high
          low: parseFloat(item.low), // low
          close: parseFloat(item.close), // close
          volume: parseFloat(item.volume)// volume
        }
      })
      res.send(chartData)
    })
      .catch(e =&gt; {
        console.log(e)
      })
  }
  </code></pre>