MongoDB: query or aggregate to get different measure data into array in one shot -
i have time series document store device's multiple sensor data/1/. in example, deviceid a-1
has
1)geo info("longitude" : 0.6644560403052914, "latitude" : 0.7165033050936503 ), 2)temperature, ("sensortype" : "temperature","sensorvalue" : "14") 3)humidity, ("sensortype" : "humidity", "sensorvalue" : "12")
the query based on time range according createdate, how use 1 query or 1 aggregation 3 arrays of geo info, temperature , humidity , how choose index , index type?
/1/data structure
{ "_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" } ] }
this isn't answer yet wanted show got. i'm little stumped doing within 1 aggregation.
> db.foo.aggregate([ ... // first project out fields don't need. geoinfo requires latitude , longitude let's take care of first. ... {"$project": {"_id": 0, "geoinfo": {"longitude": "$longitude", "latitude": "$latitude"}, "sensorlist": 1}}, ... // let's group based on sensortype, need 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 } ] }
Comments
Post a Comment