首页 > 解决方案 > 在 Mongodb 上超过了 104857600 字节的内存限制

问题描述

我必须查询一个 mongodb 数据库

我正在这样做:

 var filter = Builders<BsonDocument>.Filter.Eq("origine", sOrigin)
              & BsonDocument.Parse("{$expr: {$ne:['$crc_n', '$crc_n_1']} }");

  var sort = Builders<BsonDocument>.Sort.Ascending("update_date");

 List<BsonDocument> docs = collection.Find(filter).Sort(sort)
                .Limit(5000)
                .ToList();

但我有这个消息:

 Command find failed: Executor error during find command :: caused by :: Sort 
exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. 
Aborting operation. Pass allowDiskUse:true to opt in.."}

我不想更改限制或自定义服务器。我想通过代码进行管理。

我必须使用限制较小的线程吗?

我必须使用 ConcurrentBag 并查询我的数据库吗?

不接触服务器我能做什么?

标签: c#mongodb

解决方案


错误消息说明了解决方案:

通过 allowDiskUse:true 选择加入..

Find方法提供可选参数FindOptions,请参见查找方法

但是,不支持根据FindOptions 类选项。allowDiskUse您必须进行测试,也许只是缺少文档。

否则使用IMongoCollectionExtensions.Aggregate 方法AggregateOptions该类支持属性AllowDiskUse

只需将您的条件包围$match,即"{$match: {$expr: {$ne:['$crc_n', '$crc_n_1']} } }"

更新: 我的答案中的链接是指 C# Driver 2.6。在更新的版本allowDiskUse中支持属性:FindOptions Class (2.11)


推荐阅读