问题
之前mongodb中的数据并不是很多,然后做排序没问题,后来数据多了之后,排序时报错.错误内容如下:
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"
}
分析
看错误提示,是说mongodb在排序时,有一个内存限制,为32MB,超过之后,就会报错.那如何解决呢?
解决
看提示又说两种方式:
1.Add an index (添加索引)
就是在我们排序的字段上添加索引,例如:我们要对"age"这个字段做排序,那么我们就在这个字段山添加索引
db.collection.createIndex( { name: 1 } )
2.specify a smaller limit(对返回结果做限制)
不符合我们的要求,放弃
网友评论