Aggregation sum in Spring Data MongoDB
问题 I have MongoDB Page and Post collections. Each page document has field postIds which is array of post Ids (String objects). I want to use aggregation to count number of posts (=number of elements in array postIds) for each page. I wrote Mongo Shell aggregation function which returns exactly what I want: db.page.aggregate([ {$unwind : '$postIds'}, {$group : {_id: '$_id', 'sum': { $sum: 1}}} ]) and it returns this result: { "_id" : "3", "sum" : 3 } { "_id" : "2", "sum" : 3 } This means that