average per day mongo in lookup

Viewed 24

I have a collection (sensordatas) with data every minute. When I use this code I get all the data from de collections from sensordatas. This data is way too much. I like to have the average data per day, otherwise it will send to much data to the client. My code is

Sensor.aggregate([
{
  $match: { _id: ObjectId(req.params.id) },
},
{
  $lookup: {
    from: "sensordatas",
    as: "data",
    let: { device_id: "$device_id", datum: "$datum" },
    pipeline: [
      {
        $match: {
          $expr: {
            $and: [
              {
                $eq: ["$device_id", "$$device_id"],
              },
              { $gte: ["$datum", moment(startdate).toDate(),] },
              { $lte: ["$datum", moment(enddate).toDate(),] },
            ],
          },
        },
        
      },
      
      { $sort: { datum: -1 } },
      
    ],
  },
},

How can I print not every item but have a average per day. I hope you can help me

0 Answers
Related