报错详细信息
com.mongodb.MongoQueryException: Query failed with error code 292 and error message 'Executor error during find command :: caused by :: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting.' on server 11.51.141.63:27017
at com.mongodb.internal.operation.FindOperation.lambda$execute$1(FindOperation.java:670)
at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$2(OperationHelper.java:564)
at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:589)
at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$3(OperationHelper.java:563)
at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:589)
at com.mongodb.internal.operation.OperationHelper.withSourceAndConnection(OperationHelper.java:562)
at com.mongodb.internal.operation.FindOperation.lambda$execute$2(FindOperation.java:661)
at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:65)
at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:692)
at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:86)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:191)
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
at com.mongodb.client.gridfs.GridFSDownloadStreamImpl.getCursor(GridFSDownloadStreamImpl.java:240)
at com.mongodb.client.gridfs.GridFSDownloadStreamImpl.getChunk(GridFSDownloadStreamImpl.java:215)
at com.mongodb.client.gridfs.GridFSDownloadStreamImpl.getBuffer(GridFSDownloadStreamImpl.java:277)
at com.mongodb.client.gridfs.GridFSDownloadStreamImpl.read(GridFSDownloadStreamImpl.java:101)
解决方式(给fs.chunks
集合的files_id
字段加索引)如
db.fs.chunks.find();
db.fs.chunks.getIndexes();
# 加索引
db.fs.chunks.createIndex({files_id: 1, n: 1}, {unique: true});
加上索引后问题得以解决!
针对网上说的
1、直接修改系统限制内存大小的变量,如
use admin
db.auth("adminuser","passwd")
db.adminCommand({setParameter:1, internalQueryExecMaxBlockingSortBytes:335544320})
执行这个命令需要admin
用户认证成功,当时没有创建admin用户,也不好停掉MongoDB服务去创建admin用户,所以当时没有使用过此方式。当然不是所有的版本都有此变量,可以通过如下命令查询所有可修改的变量
db.adminCommand({getParameter: '*'})
如果查到了就可以通过以上命令修改变量值,当然如MongoDB版本是5.0以上的,也可以直接修改配置文件如
setParameter:
internalQueryExecMaxBlockingSortBytes: 335544320
2、开启使用磁盘(allowDiskUse
)如
db.getCollection('fs.chunks').aggregate([
{$group:{_id:"$title",count:{$sum:1},title:{$addToSet:"$title"},ids:{$addToSet:"$_id"}}},{$match:{count:{$gt:1}}}],
{allowDiskUse:true})
也可以从代码层面开启使用磁盘,具体可自行搜索一下。
具体的配置含义也没有说清楚,当时试了很多次还是不行。估计是某个配置项没有配好,或者是不适用GridFs
下载问题!
3、找到具体报错的集合,然后添加合适的索引。
如上面解决方式,问题得以解决。
希望能帮助到您!