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:
{data:[
{
"date": {
"$numberDecimal": "1530057600000"
},
"open": {
"$numberDecimal": "86.13"
},
"high": {
"$numberDecimal": "86.63"
},
"low": {
"$numberDecimal": "85.47"
},
"close": {
"$numberDecimal": "85.64"
},
"volume": {
"$numberDecimal": "308508"
}
},
Is there a way I can use Mongo or Mongoose to convert the above JSON query-result into what's below?
{data:[
{
"date": 1530057600000
"open": 86.13
"high": 86.63
"low": 85.47
"close": 85.64
"volume": 308508
},
I tried selecting the fields as follows ,but this didn't work.
data[i].date.$numberDecimal,
data[i].open.$numberDecimal,
data[i].high.$numberDecimal,
data[i].low.$numberDecimal,
data[i].close.$numberDecimal
Here's my Mongoose schema:
Folder - Model - Stock.js
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)
Controller
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).then(doc => {
return res.send(doc)
}).catch(e => {
console.log(e)
})
}
I am sending the data to the front end and this is what I am receiving in the browser:
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)
})
}
toString()
on JavaScript objects, and that is basically implied from any access in a string context. You canparseFloat()
on any value, which will again actually imply thetoString()
, but of course the whole point ofDecimal128
is that the values are meant to be too large and precise for afloat
. ThereforetoString()
should generally be the way. Or of course accept the extended JSON form of{ $numberDecimal: "123.45" }
as actually being the "correct" implementation indicating "type". – Addictiondate: { type: mongoose.Types.Decimal128 }
– Jakieres.send(doc)
is really just a stringified object.res.json(doc)
is the JSON stringified form of the data. At either rate, both should be callingtoString()
. Perhaps show an MCVE with current output and expected output if you think something different or still don't understand what I am saying. – Addiction