I have a collection of ProductViews.
ProductView
{
productId: \'5b8c0f3204a10228b00a1745,
createdAt: \'2018-09-
I would suggest you add the missing date client side if it's only one or two and the number of document to process is small.
That being said, the following pipeline only works on MongoDB 4.0+ but with a little effort, we can make it work in 3.6.
[
{
$group: {
_id: null,
dates: {
$push: {
$let: {
vars: {
date: {
$dateToParts: {
date: {
$toDate: "$createdAt"
}
}
}
},
in: {
$toDouble: {
$dateFromParts: {
year: "$$date.year",
month: "$$date.month",
day: "$$date.day"
}
}
}
}
}
}
}
},
{
$addFields: {
startDate: {
$divide: [
{
$min: "$dates"
},
1000
]
},
endDate: {
$divide: [
{
"$max": "$dates"
},
1000
]
}
}
},
{
$addFields: {
dates: {
$map: {
input: {
$concatArrays: [
"$dates",
{
$setDifference: [
{
$map: {
input: {
$range: [
{
$toDouble: "$startDate"
},
{
$toDouble: "$endDate"
},
24*60*60
]
},
in: {
$multiply: [
"$$this",
1000
]
}
}
},
"$dates"
]
}
]
},
in: {
$toDate: "$$this"
}
}
}
}
},
{
"$unwind": "$dates"
},
{
"$group": {
_id: "$dates",
views: {
$sum: 1
}
}
},
{
"$sort": {
_id: -1
}
}
]