聚合框架中的$ skip和$ limit [英] $skip and $limit in aggregation framework

查看:124
本文介绍了聚合框架中的$ skip和$ limit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读文档时,我发现以下注意事项:

When I read the document I found the following notes:

当管道中$ sort紧靠$ limit时,$ sort操作只会在执行过程中保持前n个结果,其中n是指定的限制,MongoDB仅需要在内存中存储n个项目.当allowDiskUse为true且n个项超过聚合内存限制时,此优化仍然适用.

When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory. This optimization still applies when allowDiskUse is true and the n items exceed the aggregation memory limit.

如果我对此表示正确,则仅当我将$ sort和$ limit一起使用时才适用

If I'm right about this, it applies only when I use the $sort and $limit together like

db.coll.aggregate([
    ...,
    {$sort: ...},
    {$limit: limit},
    ...
]);

但是,我认为大多数时候我们都会有

However, I think most of the time we would have

db.coll.aggregate([
    ...,
    {$sort: ...},
    {$skip: skip},
    {$limit: limit},
    ...
]);

问题1 :这是否意味着如果我在此处使用$ skip,上述规则将不适用?

Question 1: Does it mean the rule above doesn't apply if I use $skip here?

之所以问这个问题,是因为从理论上讲MongoDB仍然可以计算前 n 条记录并通过仅对前 n 条记录进行排序来提高性能.我没有找到任何关于此的文件.如果该规则不适用,

I ask this question because theoretically MongoDB can still calculate the top n records and enhance performance by sorting only top n records. I didn't find any document about this though. And if the rule doesn't apply,

问题2 :是否需要将查询更改为以下内容以提高性能?

Question 2: Do I need to change my query to the following to enhance performance?

db.coll.aggregate([
    ...,
    {$sort: ...},
    {$limit: skip + limit},
    {$skip: skip},
    {$limit: limit},
    ...
]);

编辑:我认为解释我的用例会使上面的问题更有意义.我正在使用MongoDB 2.6提供的文本搜索功能来查找产品.我担心如果用户输入一个非常常见的关键字(例如"red"),将返回太多结果.因此,我正在寻找产生此结果的更好方法.

EDIT: I think explains my use case would make the question above makes more sense. I'm using the text search feature provided by MongoDB 2.6 to look for products. I'm worried if the user inputs a very common key word like "red", there will be too many results returned. Thus I'm looking for better ways to generate this result.

EDIT2 :事实证明,上面的最后一个代码等于

EDIT2: It turns out that the last code above equals to

db.coll.aggregate([
    ...,
    {$sort: ...},
    {$limit: skip + limit},
    {$skip: skip},
    ...
]);

因此,我们始终可以使用此表单来应用 top n 规则.

Thus I we can always use this form to make the top n rule apply.

推荐答案

由于这是我们正在讨论的文本搜索查询,因此最佳形式是:

Since this is a text search query we are talking about then the most optimal form is this:

db.collection.aggregate([
    { 
       "$match": {
               "$text": { "$search": "cake tea" }
    }
    },
    { "$sort": { "score": { "$meta": "textScore" } } },
    { "$limit": skip + limit },
    { "$skip": skip }
])

从顶部排序"结果中得出的内存保留的原理将仅在其自身的限制"内起作用,并且对于超出几个合理页面"的数据而言,这并不是最佳选择.

The rationale on the memory reserve from the top "sort" results will only work within it's own "limits" as it were and this will not be optimal for anything beyond a few reasonable "pages" of data.

除了合理的内存消耗之外,附加阶段可能会产生负面影响,而不是正面影响.

Beyond what is reasonable for memory consumption, the additional stage will likely have a negative effect rather than positive.

这些实际上是当前形式对MongoDB可用的文本搜索功能的实际限制.但是,对于任何更详细和需要更高性能的内容,就像许多SQL全文"解决方案一样,您最好使用外部专用"文本搜索解决方案.

These really are the practical limitations of the text search capabilities available to MongoDB in the current form. But for anything more detailed and requiring more performance, then just as is the case with many SQL "full text" solutions, you are better off using an external "purpose built" text search solution.

这篇关于聚合框架中的$ skip和$ limit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆