I have this code in Javascript to get data from my MongoDB at an interval of 15mins and store in JSON. I'm not getting any errors but I'm sure I'm doing something wrong.:
test.js
var datetime = new Date();
var y = new Date();
datetime.setHours(datetime.getHours() - 168);
y.setMinutes(15);
// construct aggregation pipeline retrieving last 15 minutes
var pipeline = [
{
$group: {
_id: {
Time: "$datetime",
Humidity: { $avg: "$humidity" }
},
},
},
{
$project : {
_id : 0,
"Time": {
$range: [0, "$datetime", y]
},
Humidity: 1
}
},
{ $sort: { "_id.Time": -1, "Humidity": -1 } }
];
I will appreciate any efforts.
These are the items in my collection
[{ "_id" : ObjectId("585db63541472bf7683eb3c5"), "datetime" : ISODate("2016-12-23T23:41:41.897Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db63a41472bf7683eb3c6"), "datetime" : ISODate("2016-12-23T23:41:46.927Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db63f41472bf7683eb3c7"), "datetime" : ISODate("2016-12-23T23:41:51.956Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db644483ab4f778a635bf"), "datetime" : ISODate("2016-12-23T23:41:56.991Z"), "temperature" : 19, "humidity" : 23 }
{ "_id" : ObjectId("585db64a483ab4f778a635c0"), "datetime" : ISODate("2016-12-23T23:42:02.032Z"), "temperature" : 19, "humidity" : 23 }]
I would like to like to get a JSON data that outputs the Average humidity with an interval of 15 mins over a 1 hour period with a specific start time and end time.
[{ "_id" : ObjectId("585db63541472bf7683eb3c5"),
"datetime" : ISODate("2016-12-23T23:41:41.897Z"), // start time
"humidity" : 23 }
{ "_id" : ObjectId("585db63a41472bf7683eb3c6"),
"datetime" : ISODate("2016-12-23T23:56:46.927Z"),
"humidity" : 23 }
{ "_id" : ObjectId("585db63f41472bf7683eb3c7"),
"datetime" : ISODate("2016-12-23T23:11:51.956Z"),
"humidity" : 23 }
{ "_id" : ObjectId("585db644483ab4f778a635bf"),
"datetime" : ISODate("2016-12-23T23:26:56.991Z"), //end time
"humidity" : 23 }]