0

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

Manolait
  • 353
  • 2
  • 11
  • The query is certainly wrong. Show what actually comes in `req.query.array` and some example documents which also show the field it is expected to match against. Actually show all of `req.query` because that `greenhouse` assignment is wrong also. – Neil Lunn Jun 05 '18 at 20:42
  • As stated, you appear to have other things wrong in the query, however if you want "last 1 for each list item" or "last n for each list item" then the referenced answer is where you need to be looking. Despite the title, the actual subject is about "limiting things for a list of things" and you essentially need the same kind of approach as one of the various ones discussed there. – Neil Lunn Jun 05 '18 at 21:00
  • Ok thanks for the answer, then the valid answer would be MongoDB 3.6 Preview or Original Content? @NeilLunn – Manolait Jun 06 '18 at 11:55

0 Answers0