I've already answered similar question some time ago here however it would be good to walk you through entire solution.
Yes, you need an aggregation to reshape your data. To get started you need to convert your ObjectId values into Dates using $toDate operator. Using $dateToString will allow you to categorize these dates by day. Then you can $group them and count.
To calculate the "cummulative" part you need to get them all into single document (grouping by null
), This will allow you to run $map on $range of numbers that will be representing array indexes (0,1,2
etc). For each index you take corresponding date to the output and $sum $slice -ed array of values.
The rest is easy as you just need to run $unwind to get single document per day and $replaceRoot to get rid of nested document in your final result.
[
{ $addFields: { "date": { $dateToString: { format: "%Y-%m-%d", date: { $toDate: "$_id" }} } }},
{ $group: { _id: "$date", count: { $sum: 1 } } },
{ $sort: { _id: 1 } },
{ $group: { _id: null, days: { $push: "$_id" }, counts: { $push: "$count" } } },
{ $project: {
days: {
$map: {
input: { $range: [ 0, { $size: "$days" } ] },
in: { _id: { $arrayElemAt: [ "$days", "$$this" ] }, cumGrowth: { $sum: { $slice: [ "$counts", 0, { $add: [ "$$this", 1 ] } ] } } }
}
}
}
},
{ $unwind: "$days" },
{ $replaceRoot: { newRoot: "$days" } },
{ $sort: { _id: 1 } },
]
Using Mongo Charts you just need to map output fields into X and Y axis