Lets say I have a collection called events which consists of documents like so:-
{
"_id" : ObjectId(“56cee31d513b11ff4a8a6914”),
“eventType” : “Measurement”,
“eventReceivedTime” : ISODate(“2016-02-25T11:18:53.954Z”),
“measurements” : [
{
“eventDate” : “2015-10-31T18:11:03+0000”,
“updateState” : true,
“measurements” : {
“temperature” : 31.6,
“battery” : 50
}
},
{
“eventDate” : “2015-10-31T18:11:04+0000”,
“updateState” : true,
“measurements” : {
“temperature” : 32.6,
“battery” : 49
}
}
]
}
The key here is the measurement is an array of values. Now suppose I want to read all the temperature measurements in all documents over a certain time period. I am able to use aggregation to create a collection of just temperature values, one for each document so I can display them in a graph. So the pipeline I’m using is something like this:-
var pipeline = [
{$unwind: “$measurements”},
{$match: {“eventType” : “Measurement”}},
{$project: {"_id": 1,
“measurements.measurements.temperature”:1,
“measurements.eventDate”:1}}
];
But at the moment this isn’t reactive. I have followed examples where people make aggregations reactive but the issue I have is the ObjectID will be in some instances the same so I can’t use that as the _id. Basically when new values are added to the event collection I just want to insert it in to the temperature collection.
Any tips would be helpful.