我都存储在以下时间序列数据在MongoDB中
{
"_id" : ObjectId("59a46062e1aeb958a712490e"),
"channelName" : "ABC",
"rtData" : [
{
"ts" : ISODate("2017-08-28T18:26:42.837Z"),
"data" : [ 676.297664, 676.297664 ]
},
{
"ts" : ISODate("2017-08-28T18:27:42.837Z"),
"data" : [ 724.297664, 676.297664 ]
},
{
"ts" : ISODate("2017-08-28T18:29:42.837Z"),
"data" : [ 878.297, 676.297 ]
}
]
}
我想组基于小时的TS字段中的数据,并得到rtData的第一个元素为小时。 这是我曾尝试
db.channels.aggregate([ {$match: {"channelName": "ABC"} }, { $unwind : "$rtData" }, { $group : {_id: { $hour: "$rtData.ts" }, ucast: { $sum: $rtData.data[0]} }
但上面的代码给我下面的输出
{ "_id" : 28, "ucast" : 0 }
什么其实我想要的是
{ "_id" : 28, "ucast" : 676.297664 }
感谢您的使用。 – Neeraj
@Neeraj欢迎来到StackOverflow。不要只留下评论说谢谢,而是通过[Accepting the Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)指出正确答案是一个明确的迹象。 –