问题描述
报错信息:com.mongodb.MongoQueryException: Query failed with error code 96 and error message 'Executor error during find command :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.
百度了一下,报错原因时因为 mongo 的排序会在内存中执行排序,而 mongo默认限制了内存使用大小为 33554432B,需要排序的数据过大就会报这样的错误。
解决方法
- 扩大内存限制大小(不建议)
- 为排序字段建立索引。其目的时为了不在内存中执行排序,因为建立索引后,查询出来的数据已经是有序的了。
建立索引的语句:db.my_collection.createIndex({createTime: -1})