Mongodb getMore执行器错误:溢出排序阶段缓冲数据使用量超过内部限制

Mongodb getMore执行器错误:溢出排序阶段缓冲数据使用量超过内部限制,mongodb,mongodb-query,Mongodb,Mongodb Query,这不是一个重复的问题。所有其他答案都说解决方案是在排序键上创建索引。在我的例子中,我确实有一个索引,但仍然面临这个错误 给定一个mongodb集合,其文档类似于: { '_id': ..., 'title': ..., 'price': ..., 'category_id': ..., 'last_updated': ..., ... other keys } 我在category\u id上有一个上升的单字段索引,在last\u updated

这不是一个重复的问题。所有其他答案都说解决方案是在排序键上创建索引。在我的例子中,我确实有一个索引,但仍然面临这个错误

给定一个mongodb集合,其文档类似于:

{
    '_id': ...,
    'title': ...,
    'price': ...,
    'category_id': ...,
    'last_updated': ...,
    ... other keys
}
我在
category\u id
上有一个上升的单字段索引,在
last\u updated
上有一个下降的单字段索引

以下查询崩溃:

> var c = db.collection_name.find({category_id: "categ_id"}, {_id: 0, price: 1, title: 1}).sort({last_updated: -1}).limit(20000).batchSize(500)
> c.forEach(function(doc) {
... ;
... })
2015-05-13T10:00:46.561+0000 E QUERY    Error: error: {
        "$err" : "getMore executor error: Overflow sort stage buffered data usage of 33554596 bytes exceeds internal limit of 33554432 bytes",
        "code" : 17406
}
    at Error (<anonymous>)
    at DBQuery.next (src/mongo/shell/query.js:259:15)
    at DBQuery.forEach (src/mongo/shell/query.js:414:20)
    at (shell):1:3 at src/mongo/shell/query.js:259
有趣的是,这种错误只发生在特定类别上,而不是所有类别。此外,如果我删除
batchSize
选项,查询不会崩溃(无论我为批设置的大小如何)


值得注意的是,
last\u updated
字段可能并不存在于所有文档中。

因此,我的问题中的查询解释中提供了线索。由于查询中正在使用
category\u id
,查询优化器选择使用
category\u id
索引,并完全忽略上次更新的
索引。我的想法是,它将使用
category\u id
进行抓取,并使用
last\u updated
进行排序,但这似乎不是mongodb查询的工作方式。为了解决这个问题,需要为
category\u id
last\u updated
创建一个复合索引:

db.collection_name.createIndex({category_id: 1, last_updated: -1})
db.collection_name.createIndex({category_id: 1, last_updated: -1})