Efficient pagination of MongoDB aggregation?

后端 未结 3 829
北海茫月
北海茫月 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/

    0 讨论(0)
  • 2021-02-15 13:07

    MongoDB recommendation of $sort preceding $limit is absolutely true as when it happens it optimizes the memory required to do the operation for top n results.

    It just that the solution you proposes doesn't fit your use case, which is pagination.

    You can modify your query to to get the benefit from this optimization.

    collection.aggregate([
      {
        $unwind: u
      }, 
      {
        $group: g
      },
      {
        $match: f
      }, 
      {
        $sort: s
      }, 
      {
        $limit: l+p
      },
      { 
        $skip: p
      }
    ]);
    
    

    or for find query

     collection.find(f).sort(s).limit(l+p).skip(p)
    
    

    Though, as you can see the with big pagination the memory will grow more and more even with this optimization.

    0 讨论(0)
  • 2021-02-15 13:29

    In MongoDB cursor methods (i.e. when using find()) like limit, sort, skip can be applied in any order => order does not matter. A find() returns a cursor on which modifications applied. Sort is always done before limit, skip is done before limit as well. So in other words the order is: sort -> skip -> limit.

    Aggregation framework does not return a DB cursor. Instead it returns a document with results of aggregation. It works by producing intermediate results at each step of the pipeline and thus the order of operations really matters.

    I guess MongoDB does not support order for cursor modifier methods because of the way it's implemented internally.

    You can't paginate on a result of aggregation framework because there is a single document with results only. You can still paginate on a regular query by using skip and limit, but a better practice would be to use a range query due to it's efficiency of using an index.

    UPDATE:

    Since v2.6 Mongo aggregation framework returns a cursor instead of a single document. Compare: v2.4 and v2.6.

    0 讨论(0)
提交回复
热议问题