Here see this Collection here have a record with createdAt with 14Feb and one with 16Feb, Now querying an aggregation on the collection such as I want to add an document of 15Feb as it was missing in collection with viewCount and clickCount as 0.
So looking for some way to add missing date record/document in aggreagation pipeline.
Pipeline I have
[
{
$group: {
_id: {
$dateToString: { format: "%Y-%m-%d", date: "$createdAt" },
},
viewCount: { $sum: "$viewCount" },
clickCount: { $sum: "$clickCount" },
},
},
{ $sort: { _id: 1 } },
{
$group: {
_id: null,
data: {
$push: {
k: "$_id",
v: { viewCount: "$viewCount", clickCount: "$clickCount" },
},
},
},
},
{
$project: {
_id: 0,
data: { $arrayToObject: "$data" },
},
},
]```
I looking for some operator in mongo or some way to do this
2
Answers
You can use
$densify
and$fill
stages to fill gaps in your data. As a sample, I have changed your pipeline to not group by strings, but use$dateTrunc
to cut of the dates after the day, so all you can group by day but still use anISODate
for easier interpolation afterwards.Please note that this sample pipeline does only contain the first of the stages of your pipeline and can serve as a starting point. In order to show how you can fill in missing values, I have stopped at this point:
Use this mongoplayground to test.
By combination of both javascript and aggregation pipeline, you can achieve the desired result.