Same issue. Simple group by $dateTrunc, then $densify. If your timezone is not 0, the resulting data has many extra dates. Seems like densify algorithm does not matches the $dateTrunc
Example of code. pay attention to timezone
{
"$match" : {
"createdAt" : {
"$gt" : ISODate("2023-12-31T22:00:00.000+0000")
}
}
},
{
"$group" : {
"_id" : {
"$dateTrunc" : {
"date" : "$createdAt",
"unit" : "month",
"timezone" : "+0200"
}
}
}
},
{
"$densify" : {
"field" : "_id",
"range" : {
"step" : 1.0,
"unit" : "month",
"bounds" : [
ISODate("2023-12-31T22:00:00.000+0000"),
ISODate("2024-12-31T22:00:00.000+0000")
]
}
}
}