I do have a MongoDB collection that stores values from some Airflow sensor. For putting these values into a line chart, I need these values grouped together into 15 minute intervals. My aggregation query does return the correct values, but I need them to include the timestamp.
The entries in this collection look like this:
{
"_id":{"$oid":"650dd2e15013d08ec65416da"},
"time":{"$numberLong":"1695404767436"},
"tenantId":"someTenantIdString",
"sensorId":"someSensorIdString",
"VolumetricFlowRate":{
"value":{"$numberDouble":"0.0"},
"unit":"m3/h",
"max":{"$numberDouble":"84.21"},
"mean":{"$numberDouble":"0.025"},
"min":{"$numberDouble":"0.0"}}
My code for the aggregation function looks like this:
async getAllValuesFromRange(
sensorId: string,
tenantId: string,
from: number,
to?: number,
) {
const fifteenMinutesInMilliseconds = 1000 * 60 * 15;
let allValuesFromRange = this.processGasModel
.aggregate([
{
$match: {
time: {
$gte: from,
...(to ? { $lte: to } : {})
},
tenantId,
sensorId
}
},
{
$project: {
_id: 0,
time: -1,
lpermin: { $multiply: ["$lpermin", ProcessgasService.DATA_MEASUREMENT_CONVERSION_RATIO] },
}
},
{
$group: {
_id: {
$toDate: {
$subtract: [
"$time",
{ $mod: ["$time", fifteenMinutesInMilliseconds] }
]
}
},
totalLiters: { $sum: "$lpermin" },
timestamp: { $first: "$time" }
}
},
{
$sort: { _id: 1 }
},
{
$project: {
time: "$_id",
_id: 0,
liters: "$totalLiters"
}
},
]);
allValuesFromRange.then((result) => {
console.log("AllValuesFromRange: " + result);
})
return allValuesFromRange;
}
I want the output within 'allValuesFromRange' to look like this:
{ time: 1718717978632, liters: 483 },{ time: 1718717918630, liters: 275 }
where each object represents the grouped values of 15 minutes.