I need to recover the last value of a list of sensors that happened to my Backend in the form of an array.
I am not clear how to do this, I have tried to calculate the size of my Array and put this limit (x), the problem of this is that the day a sensor does not insert a data, I am already entering a value that is not correct .
There is some way in Node JS to make a query N times for each value of my array with limit (1) and then return the total of the data.
I'm working with MongoDB.
This is my code:
function getDataSensorGreenhousetypeFechas (req, res) {
var arr = JSON.parse (req.query.array);
greenhouse var = greenhouse req.query;
var dateinicio = req.query.fechainicio;
var fechafin = req.query.fechafin;
var counter = arr.length;
Datagreenhouse.find ({$ and: [{timestamp: {"$ gte": start date}}, {timestamp: {"$ lte": end date}}, {sensor: arr, greenhouse: greenhouse}])
.populate ('sensor')
.sort ({timestamp: -1})
.limit (counter)
.exec ((err, DatagreenhouseRecuperado) => {
if (err) return res.status (500) .send ({message: 'Error when requesting' + err})
if (! DatagreenhouseRecovered) return res.status (404) .send ({message: 'Error the user does not exist'})
res.status (200) .send ({Datagreenhouse: {DatagreenhouseRecuperado}})
})
}
Any help is good