title: How to get records of mongodb for every hour automatically?
body: <p>I've a Node.JS application. It gets the temperature data from the devices for every second and stores in the MongoDB. Now I need to find the data of last 24 months and need to get the average temperature of every hour of last 24 hours. I've got last 24 hours data. But couldn't able to find a way to get the average data/hour. Is there any approach to achieve the solution?</p>
  
  <pre><code>mongoose.connection.db.collection(templateId)
         .aggregate([
                     {
                       $match: {
                                "deviceId":deviceId,
                                "entryDayTime":{
                                    $lt : new Date(),
                                    $gte: new Date(new Date().setHours(new Date().getHours()-24))
                                       }
                               }
                     },
                     {
                       $sort: {"entryDayTime":-1}
                     },
                     {
                       $group: {
                                 _id: null,
                                 average: {$avg:"$temperature"},
                               }
                     },
                     {
                       $project:{
                                 _id:0,average:1
                                }
                     }]
  </code></pre>
  
  <p>This is the aggregation code I've written so far to get last 24 hours data.</p>