【问题标题】:MongoDB C# Driver 'Cursor not found' with Linq query带有 Linq 查询的 MongoDB C# 驱动程序“找不到光标”
【发布时间】:2016-10-06 17:25:52
【问题描述】:

我正在尝试在 Mongo 数据库中进行选择 我正在使用这个 DLL

MongoDB.Bson,MongoDB.Driver,MongoDB.Driver.Linq

我的表有超过 55k 行

一段时间后出现此错误

找不到光标

这是我的代码

var client = new MongoClient(connectionString);        
var server = client.GetServer();        
var database = server.GetDatabase("Database");
var collection = database.GetCollection<DesktopSessions>("desktop_sessions");
var query = (from e in collection.AsQueryable<DesktopSessions>()
            where e.created_at > new DateTime(2012, 7, 1)
            select e);
foreach (var item in query)
{
    string id = item._id.ToString();
}

我该如何解决这个问题?

【问题讨论】:

  • 如果先把 new DateTime() 放到一个变量中,再运行查询,效果会更好吗?
  • 一些时间是多少时间?您可能会在光标上超时
  • @I3arnon 游标的默认超时为 10 分钟。除非设置了noTimeout 标志。
  • 如何更改超时时间?该过程需要10多分钟
  • 请注意,这是 10 分钟不活动,因此如果光标正在迭代,它不会超时。它可能有助于添加更多关于循环中发生的事情以及实际时间、迭代行等的具体信息。

标签: c# linq mongodb mongodb-.net-driver


【解决方案1】:

我把代码改成了这个

 var collection = database.GetCollection<DesktopSessions>("desktop_sessions");
 var queryM = Query.GTE("created_at", new BsonDateTime(new DateTime(2012,7,1)));
 var cursor = collection.Find(queryM);
 cursor.SetFlags(QueryFlags.NoCursorTimeout);

有效!!

【讨论】:

  • 如果没有耗尽游标,服务器永远不会清理它,这将是资源泄漏,直到重新启动mongod进程才能清理资源。用尽光标或找到没有NoCursorTimeout 的方法很重要。
【解决方案2】:

其他选项是为整个数据库设置超时,这就是我正在做的。 您可以在配置、命令行、mongo shell 甚至 C# 中执行此操作。

请看这里: https://jira.mongodb.org/browse/SERVER-8188

这是我目前在初始化类中使用的解决方案

var db = this.MongoClient.GetDatabase("admin");
var cmd = new BsonDocumentCommand<BsonDocument>(new BsonDocument {
  { "setParameter", 1 },
  { "cursorTimeoutMillis", 3600000 } 
});
db.RunCommand(cmd);

更多信息可以在这里找到: https://docs.mongodb.com/v3.0/reference/parameters/#param.cursorTimeoutMillis

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2016-10-18
    • 1970-01-01
    • 2016-08-03
    • 2012-03-04
    • 2012-10-02
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多