我正在尝试使用MongoDB Aggregate框架以15分钟的间隔从我的JSON响应中获取平均温度和湿度。我得到了SyntaxError: invalid property id @(shell):2:0
这是我的代码
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}}
])
这是我的JSON回复
[{ "_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 }]
我也得到一个空数组
谢谢:)