I have a mongoDB structure that looks like this:
values : { [
oneValue : {
number: '20'
unit: 'g'
}
differentValue : {
number : '30'
unit : 'g'
}
]}
I am using node js this is what I do:
doc.values.forEach(function(err, idx) {
var object = doc.values[idx];
}
And what ends up happening is I can get an object that looks like this:
object = oneValue : {
number: '20'
unit: 'g'
}
But node does not recognize it as a JSON because when I try to do JSON.parse(object) it doesn't know how to handle it.
I want to be able to get at the number field dynamically. So I don't want to say doc.values[idx].oneValue because this is a pretend case and in the real case oneValue could be one of 1000 different things. Does anyone know how I can access the 'number' field with this structure?
figured it out...
after
var object = docs.values[idx]
do this:
var objAsJson = JSON.stringify(object);
JSON.parse(objAsJson, function(k, v) {
console.log(k + " " + v);
});
That will print out all the data in the embedded object and you don't have to know the name.