Limit number of items in each group (aggregate)

2,576 views
Skip to first unread message

Billy Newman

unread,
Jul 21, 2013, 6:38:52 PM7/21/13
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?

Asya Kamsky

unread,
Jul 24, 2013, 2:44:19 AM7/24/13
to mongodb-user
Unfortunately there isn't a way to do this at the moment.  

You might want to vote up https://jira.mongodb.org/browse/SERVER-9377 which is I think what you want to have.





--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages