I am currently in the process of utilizing a sensor to measure temperature, which is then stored in a mongo database. This data can be accessed as a JSON file by visiting ('/data').
My goal is to determine the latest entry and extract the temperature value from it.
This is an example of how the JSON data appears (x10,000):
{
"_id": {
"$oid": "5882d98abd966f163e36a6cf"
},
"temperature": 19,
"humidity": 30,
"epochtime": 1484970377120
}
I have configured the website with express.js, and I presume that I will need to parse this JSON object to navigate through it and identify the most recent entry.
for ("temperature" in '/data') {
output += temperature + ': ' + obj[temperature]+'; ';
}
console.log(output[0]);
Thank you for your assistance.
UPDATE & Solution:
A big thank you to Bertrand Martel for providing an almost flawless solution.
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
db.collection('Sensors', function(err, collection){
assert.equal(err, null);
var options = {
"limit": 1
}
var sort = {
"epochtime": -1
}
collection.find({}, options).sort(sort).toArray(function(err, res) {
assert.equal(err, null);
console.log("most recent temperature : " + res[0].temperature);
console.log("most recent humidity : " + res[0].humidity);
});
});
db.close();
});