Efficient pagination of MongoDB aggregation?

后端 未结 3 841
北海茫月
北海茫月 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: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.

提交回复
热议问题