I have a collection in which each document looks something like this:
{
_id: 'dev_id:datetime_hour',
data: {
0: {
0: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
},
1: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
},
2: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
},
59: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
}
},
1: {
0: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
},
1: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
},
2: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
},
59: {
voltage_a: float,
voltage_b: float,
voltage_c: float,
current_a: float,
current_b: float,
current_c: float,
current_n: float,
active_power_a: float,
active_power_b: float,
active_power_c: float,
total_active_power: float
}
}
}
I've simplified it here but the fundamental idea is: sensor data is stored every second but is bundled together by the hour. The 'data' field indexes these by the minute and each minute indexes by the second. Therefore a full hours worth of data would yeild 3600 entries in the nested data field. For example to get the sensor data for the first minute and third second I could access the object directly: data.1.3.
This type of schema was recommended for storing time series data by MongoDB.
Stage 1 of my aggregation pipeline looks like this:
db.raw_electric.aggregate(
[
// Stage 1
{
$match: {
_id: { $regex: /^r10a:/ },
datehour: {$gte: ISODate("2016-09-21T17:00:00"), $lte: ISODate("2016-09-21T19:00:00")}
}
}
]
);
Is it possible to 'unwind' the document - similar to how you'd unwind an array so that I can expose each of the nested layers of an object?
$unwindoperator, it's only applied to arrays.