I'm attempting to calculate the average temperature and humidity from my JSON response at 15-minute intervals using the MongoDB Aggregate framework. However, I'm encountering a
SyntaxError: invalid property id @(shell):2:0
This is the code I am using:
var datetime = new Date();
var interValue = 15*1000*60;
datetime.setHours(datetime.getHours() - 1 + interValue);
var endtime = new Date();
db.sensordata.aggregate([
{ $match : { datetime : { $gte : datetime, $lt: endtime } } },
{
$group: {
_id: {
Time: "$datetime",
Humidity: { "$avg" : "$humidity" },
Temperature: { "$avg" : "$temperature"}
},
},
},
{
$project :{ _id : 1 , Humidity : 1, Temperature: 1 } },
{ $limit : 10 },
{ $sort : {"_id.Time":1, "Humidity":1, "Temperature": 1}}
])
Here is my JSON response:
[{ "_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 }]
Additionally, I am receiving an empty array.
Thank you :)