我有一个时间序列文档来存储设备的多个传感器数据/ 1 /。在示例中,deviceId A-1
具有
1)geo info("longitude" : 0.6644560403052914, "latitude" : 0.7165033050936503 ),
2)temperature, ("sensorType" : "temperature","sensorValue" : "14")
3)humidity, ("sensorType" : "humidity", "sensorValue" : "12")
查询基于createDate的时间范围,如何使用一个查询或一个聚合来获取三个地理信息,温度和湿度数组以及如何选择索引和索引类型?
/ 1 /数据结构
{
"_id" : "7736b113586a4314bfbe036cf6b3e34c",
"createDate" : ISODate("2016-08-30T16:30:33.007Z"),
"deviceId" : "A-1",
"vendorCode" : "01",
"longitude" : 0.6644560403052914,
"latitude" : 0.7165033050936503,
"sensorList" : [
{
"_id" : "1802b2792cac4529b269d5e2c9398e80",
"createDate" : ISODate("2016-08-30T16:30:33.007Z"),
"sensorType" : "temperature",
"sensorId" : 1,
"sensorValue" : "14"
},
{
"_id" : "ddbe2d76770c42cfabc8d22d7c732826",
"createDate" : ISODate("2016-08-30T16:30:33.007Z"),
"sensorType" : "humidity",
"sensorId" : 2,
"sensorValue" : "12"
}
]
}
答案 0 :(得分:0)
这还不是答案,但我想告诉你我在哪里。我有点难以在一个聚合中完成这一切。
> db.foo.aggregate([
... // First project out the fields we don't need. geoInfo only requires latitude and longitude so let's take care of it first.
... {"$project": {"_id": 0, "geoInfo": {"longitude": "$longitude", "latitude": "$latitude"}, "sensorList": 1}},
... // Now let's group based on the sensorType, we need to unwind our array first
... {"$unwind": "$sensorList"},
... {"$group": {
... "_id": "$sensorList.sensorType",
... "value": {"$first": "$sensorList.sensorValue"},
... "geoInfo": {"$push": "$geoInfo"}
... }}
... ]);
{ "_id" : "humidity", "value" : "12", "geoInfo" : [ { "longitude" : 0.6644560403052914, "latitude" : 0.7165033050936503 } ] }
{ "_id" : "temperature", "value" : "14", "geoInfo" : [ { "longitude" : 0.6644560403052914, "latitude" : 0.7165033050936503 } ] }