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 that sum up 7 days value (with a timestamp starting from 2024-12-01). However, the result is not what I expected.
It would be great if anyone could provide guidance on this issue. Thank you in advance!
UPDATE
Some information about timeseries collection:
- timeField ->
timestamp
- metaField ->
tags
- expireAfterSeconds -> undefined
- granularity ->
Hours
- bucketMaxSpanSeconds -> undefined
2
Answers
Obviously 1970-01-01 is not the starting point. When you use
unit: 'day'
withbinsize
, the starting point seems to be quite random and not deterministic:I don’t see any rule!
Depending on your requirements, you need either
unit: 'week', startOfWeek: 'monday'
or you may have a look at $setWindowFields if you are looking for a sliding window.The $dateTrunc operator calculates the bin for the specified date according to its offset from a base date. The base date will be 2000-01-01 for any unit except "week".
The basic process would be:
Using the test sequence lifted from another answer, this method gives the same result: