1. 程式人生 > >MongoDB排序錯誤:Sort operation used more than the maximum 33554432 bytes of RAM

MongoDB排序錯誤:Sort operation used more than the maximum 33554432 bytes of RAM

錯誤描述

我用如下命令查詢某一個鍵的最大值:

db.video_info.find().sort({'vid':-1})

出現如下錯誤:

Error: error: {
        "ok" : 0,
        "errmsg" : "Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",
        "code" : 96,
        "codeName" : "OperationFailed"
}

分析解決問題

排序操作使用超過最大33554432個位元組的RAM,新增索引或指定較小的限制。

這是由於mongodb排序的時候會把資料載入到記憶體中,在這裡排序的資料量太大導致超過了32M的預設排序記憶體

由於我這裡僅僅需要查詢最大值,所以我採用提示的第二種方案,命令後面加一個limit

db.video_info.find().sort({'vid':-1}).limit(1)