so I have this bunch of code:
MONGO_PIPE = [
{
"$match": {"dt": {"$gte": start, "$lte": end}},
},
{
"$group": {
"_id": {"$dateTrunc": {"date": "$dt", "unit": unit}},
"value": {"$sum": "$value"},
},
},
{"$sort": {"_id": 1}},
{
"$group": {
"_id": None,
"dataset": {"$push": "$value"},
"labels": {
"$push": {
"$dateToString": {"date": "$_id", "format": "%Y-%m-%dT%H:%M:%S"}
}
},
}
},
{"$unset": "_id"},
]
In few words: it aggregates date, that matches range of dates start-end, and grouped by unit (day, week, hour, whatever)
My output now if unit=day:
{
"dataset": [1, 2, 3],
"labels": [day3, day4, day5] #example dates
}
What I need is to generate zero values for day1, day2. There are no documents matching dates day1 and day2. So output should look like this:
{
"dataset": [0, 0, 1, 2, 3],
"labels": [day1, day2, day3, day4, day5] #example dates
}
I know that $match is insufficient here, and I need to generate offset based on unit.
I am like 5 hours in this DB for the first time.
If somebody knows the answer – please let me know, thanks in advance