I have a query in mongodb with aggregation framework, which returns number of topics in my documents. This is done by utilizing the $group operator and the result looks like:
{
postsByTopics: [
{
"_id" : "Topic 1",
"count" : 3.0
},
{
"_id" : "Topic 2",
"count" : 1.0
}
]
}
I use projection to get a better shaped results
{
"$project": {
"postsByTopics": {
"$arrayToObject": {
"$map": {
"input": "$postsByTopics",
"as": "el",
"in": {
"k": "$$el._id",
"v": "$$el.count",
}
}
}
},
}
which returns much nicer shaped result:
{
postsByTopics: {
"Topic1" : 3.0,
"Topic2" : 1.0
}
}
Now I would like to turn this projection in a java spring boot code. I found
project().and(ArrayOperators.ArrayToObject.arrayToObject("postsByTopics"))
and
project().and(VariableOperators.Map.itemsOf("postsByTopics").as("el")
but I am struggling to combine them to achieve the desired result