The documentation seems to imply that this (aggregation) will fail if the match returns a result that is larger than the working memory. It's true?
No. You can, for example, populate a collection that has more physical memory without even using the $match operator. It may be slow, but it should work. No problem if $match returns what is greater than RAM.
Here are the actual pipeline limits.
http://docs.mongodb.org/manual/core/aggregation-pipeline-limits/
The $match statement does not cause memory problems. As stated in the documentation, $group and $sort are common villains. They are cumulative and may require access to the entire set of input data before they can produce any output. If they load too much data into physical memory, they will fail.
If so, is there a better way to perform pagination in the result set returned by aggregation?
I correctly said that you cannot "paginate" (apply $skip and $limit ) to the aggregation result, because it is just a MongoDB document. But you can “paginate” into intermediate results of the aggregation pipeline.
Using $limit in the pipeline will help to keep the result set within 16 MB, the maximum size of a BSON document. Even if the collection grows, you should be safe.
Problems can arise with the help of $group and, especially, $sort . You can create sort indexes to handle them if they really happen. See the documentation on indexing strategies.
http://docs.mongodb.org/manual/tutorial/sort-results-with-indexes/
Finally, keep in mind that $skip does not help in performance. On the contrary, they tend to slow down the application, since it forces MongoDB to scan every skipped document to reach the desired point in the collection.
http://docs.mongodb.org/manual/reference/method/cursor.skip/