mongodb报错一例

开发程序报错信息:

Caused by: com.mongodb.MongoException: Executor error:

  OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.

从程序报错中可以看到是排序的内存不足。

解决办法:3.x版本

  use admin

  db.adminCommand({getParameter:"*"})    #查看参数的配置

  db.adminCommand({setParameter:1, internalQueryExecMaxBlockingSortBytes:335544320})  #修改内存为排序为320M

其他解决方案:(通过创建索引方式)  

  db.你的collection.createIndex({"你的字段": -1}),此处 -1 代表倒序,1 代表正序;

  db.你的collecton.getIndexes();

  

参考官方文档:

  https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort

  https://docs.mongodb.com/manual/tutorial/optimize-query-performance-with-indexes-and-projections/

对于以上问题推荐解决方案:

  1.优化查询和索引。
  2.减少输出列(限制输出列个数)或行(如limit函数,或限制输入查询_id数量)。
  3.将查询分2步,第1步只输出_id,第2步再通过_id查明细。
  都可以解决内存中排序溢出问题。

原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/8324926.html