0

I am new to Mongodb. Please forgive me if the question is irrelevant or so and Please try to give other technique if possible.

Is there any way to get 10 documents from the Aggregate() query at first time and next time using the same Aggregate() but skipping the first 10 results fetched previously.

Please suggest: Can this avoid the limitation of max 16 mb results from aggregation as I have a large collection of documents and want to aggregate by splitting into groups

1 Answers1

1

You can use Limit and skip in aggregate query.

.aggregate([{$match:{ABC:"ABC"}},{$limit:10}])

This query will return first 10 documents and then you can use skip in next query to return all other documents.

.aggregate([{$match:{ABC:"ABC"}},{$skip:10}])

16MB is the size restriction for each document and not entire result set.

Ricky
  • 155
  • 1
  • 7
  • Thanks for the answer. U solved my confusion and I need a suggestion: Can I use this method to obtain shares and posts from 2 collections by using $lookup and return only first 10 results and when user reaches bottom of page the again use the same query and so on – Sarthak Patidar Sep 17 '17 at 13:14
  • I don't think using aggregation query like this is a good idea. I have used aggregation only for backend jobs which runs once in a day. If you don't have any other option test your query first with maximum data that you are expecting and check how much time it is taking. – Ricky Sep 17 '17 at 13:28
  • Can u look at [this one](https://stackoverflow.com/questions/46262800/mongodb-schema-for-posts-and-shares) and suggest how can I make this more effecient – Sarthak Patidar Sep 17 '17 at 13:30