Billy Newman
unread,Jul 21, 2013, 6:38:52 PM7/21/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongod...@googlegroups.com
I have a collection that contains reports for users. Each document contains a userId (ObjectId) and some properties for the report itself. I would like to group the reports by user, which is simple enough using the aggregate function. However I would also like to be able to limit the number of reports for each user in the group.
I.E. given X users each with N reports I would like to group by user, but only get the 10 newest reports for each user. This query is going to be UI driven so I would like the ability to dynamically control the number of reports I return for each user in the group.
I will have millions of reports (indexed by timestamp). I know what I am after is possible with map reduce, however due to the aggregate function running outside javascript in the mongo core I would like to take advantage of the tremendous speed up as this query will be run every few seconds/minutes.
I have tried $limit in the pipeline but I am not sure there is a good way to use it. Seems I am only able to limit the number of reports that come back not the number of reports that come back per user after I have grouped by user.
Any ideas?