These are chat archives for Automattic/mongoose

3rd
Aug 2016
Callum Bryant
@Nixes
Aug 03 2016 05:30

So I have a question about how to run a distinct query on a query result. For example I have the following schema:

var dataSchema = mongoose.Schema({
  value: Number,
  collection_time: { type: Date, default: Date.now }
});

var sensorSchema = mongoose.Schema({
    name: String,
    description: String,
    data_type: String,
    data: [dataSchema]
});

and I query for a specific sensor object with:

  Sensor.findById(req.params.sensor_id,"data", function(err,sensor){
    if (err) {
      console.log("Sensor not registered err: "+err);
      res.send({success:false});
    } else {
      res.send(sensor.data);
    }
  });

but I want to return only the data points from the sensor determined above that have a collected_at date greater than a week ago.

Something like (which doesn't work):

 Sensor.findById(req.params.sensor_id,"data", function(err,sensor){
    if (err) {
      console.log("Sensor not registered err: "+err);
    } else {
      sensor.data.find( { "collection_date": { $gt: Date.now() - week } } ).exec(function(err,data){
        res.send(data);
     })
    }
  });

Any ideas? Or am I going about this the wrong way?

Callum Bryant
@Nixes
Aug 03 2016 05:50
or should I just filter the data manually in js?
Felipe Mikio de Vasconcelos Monobe
@FelipeMonobe
Aug 03 2016 16:45
hey, I'm trying to populate a virtual schema property... nothing fancy. But when my document hits the frontend, the prop isn't in the object