Search code examples
mongodbmongooseaggregation-frameworkmongoose-populate

Mongodb: Documents in pairs of 10 sequentially from $aggregate


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


Solution

  • 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.