Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following aggregate:

db.reports.aggregate({ $group: { _id: "$user", reports: { $push: {report: {properties: "$properties"} } }}}, { $project: { _id: 0, user: "$_id", reports: "$reports"} })

I would like to somehow limit the number of reports that get pushed into the array. I.E I want to limit reports to a users defined value. 1,5,10 etc. Meaning I want N number of reports (I will sort by timestamp) to be included in the reports field. Right now I am pushing all reports not sure how to limit the number of reports per user.

I tried the $limit, but that didn't work as it restricted the number of users returned not the number of reports pushed into the reports array.

Basically I want to group my reports by user, and include only a certain number of reports. I would like to limit the number of reports as early as possible such that I do not have to deal with the entire collection throughout the pipeline

share|improve this question
You can easily do it with mapReduce. I am not sure that you can simply do it with aggregate. the $slice operator is unfortunately not supported in the aggregation for now. – innoSPG 9 hours ago
Is there a way to limit the number if reports per user in some other fashion with aggregate? IE if I was not pushing into an array. Maybe do a couple groups in the pipeline. – forumuser 5 secs ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.