I found this resource very useful when working on converting from SQL to Mongo SQL to a Mongo map map
In this case, you look like something similar to:
db.users.group({key: {age: true}, initial: {count: 0}, reduce: function (obj, prev) { prev.count++;} } )
Since you have more complex logic, you need to use the reduce function. Honestly, you need to read a little more to fully understand what is happening here. Reduce map
source share