Efficient pagination of MongoDB aggregation?

后端 未结 3 831
北海茫月
北海茫月 2021-02-15 12:25

For efficiency, the Mongo documentation recommends that limit statements immediately follow sort statements, thus ending up with the somewhat nonsensical:

 colle         


        
3条回答
  •  离开以前
    2021-02-15 13:06

    The documentation seems to imply that this (aggregation) will fail if the match returns a result set that's larger than working memory. Is this true?

    No. You can, for example, aggregate on a collection that is larger than physical memory without even using the $match operator. It might be slow, but it should work. There is no problem if $match returns something that is larger than RAM.

    Here are the actual pipeline limits.

    http://docs.mongodb.org/manual/core/aggregation-pipeline-limits/

    The $match operator solely does not cause memory problems. As stated in the documentation, $group and $sort are the usual villains. They are cumulative, and might require access to the entire input set 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 on a result set returned through aggregation?

    I has been correctly said that you cannot "paginate" (apply $skip and $limit) on the result of the aggregation, because it is simply a MongoDB document. But you can "paginate" on the intermediate results of the aggregation pipeline.

    Using $limit on the pipeline will help on keeping the result set within the 16 MB bounds, the maximum BSON document size. Even if the collection grows, you should be safe.

    Problems could arise with $group and, specially, $sort. You can create "sort friendly" indexes to deal with them if they do actually happen. Have a look at the documentation on indexing strategies.

    http://docs.mongodb.org/manual/tutorial/sort-results-with-indexes/

    Finally, be aware that $skip does not help with 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/

提交回复
热议问题