You can use this aggregate to start with and refine it as per your formatting:
db.collection.aggregate([
{
$match: { _id: INPUT_USER_ID }
},
{
$group: {
_id: { year: { $year: "$date_started" }, month: { $month: "$date_started" } },
total_cost_month: { $sum: "$total_cost" }
}
}
])
A sample output for a year+month would be:
{
"_id" : { "year" : 2021, "month" : 6 },
"total_cost_month" : 16.8
}