I'm not used to work with Spring Data and I'm trying to do this MongoDB aggregation but I'm not able to solve the project and group part, match part was pretty easy:
db.collection.aggregate(
{ $match: { "car._id": "abc1234" } },
{
$project: {
month: { $month: "$day" },
year: { $year: "$day" },
services: 1
}
},
{
$group: {
_id: { month: "$month", year: "$year" },
total: { $sum: "$services" }
}
}
)
day is a Date type field. The query is working fine on the mongo shell, filtering by _id and grouping by year and months with the sum of all services (Int field). But I'm not able to implement it on Spring Data MongoDB.
I've tried with the Aggregation.group() but I'm getting lost because of the nested object in the _id.