Extract Decimal from Decimal128 with Mongoose - MongoDB

前端 未结 3 1079
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 13:25

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 que

3条回答
  •  自闭症患者
    2021-01-19 14:19

    Working solution

    const mongoose = require('mongoose')
    const parentSchemaSymbol = mongoose.model('Stock')
    
    exports.dbFetch = (req, res) => {
      let curValueDbFetch = req.params.symbol
    
      const query = { symbol: `${curValueDbFetch}` }
      const projection = { _id: 0, data: 1 }
    
      parentSchemaSymbol.findOne(query, projection).sort({ date: -1 }).then(doc => {
        let chartData = doc.data.map(item => {
          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 => {
          console.log(e)
        })
    }
    

提交回复
热议问题