I was using $dateTrunc on a timeseries collection to group the timeseries data from 2024-12-01 to 2024-12-07 into a single bin.
Here is the aggregate pipeline:
[
{
$match: {
timestamp: {
$gte: ISODate('2024-12-01'),
$lte: ISODate('2024-12-07'),
},
}
},
{
$group: {
_id: {
timestamp: {
$dateTrunc: {
date: "$timestamp",
unit: 'day',
binSize: 7,
},
}
},
count: {
$sum: 1
}
}
},
{
$set: {
timestamp: "$_id.timestamp"
}
},
{
$sort: {
timestamp: 1
}
}
]
Here is the result:
[
{
"_id": {
"timestamp": {
"$date": "2024-11-30T00:00:00.000Z"
}
},
"count": 719,
"timestamp": {
"$date": "2024-11-30T00:00:00.000Z"
}
},
{
"_id": {
"timestamp": {
"$date": "2024-12-07T00:00:00.000Z"
}
},
"count": 116,
"timestamp": {
"$date": "2024-12-07T00:00:00.000Z"
}
}
]
I expect the aggregation pipeline to return a single data point (from 2024-12-01 to 2024-12-07). However, the result is not what I expected.
It would be great if anyone could provide guidance on this issue. Thank you in advance!