MongoDB C# Driver 'Cursor not found' with Linq query

I'm trying to do a select at a Mongo database I'm using this DLL

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

My table have more than 55k rows

After some time occurs this error

Cursor Not Found

Here is my code

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();
}

How can I solve this problem?

Upvotes: 1

Views: 2758

Answers (2)

Jiř&#237; Hern&#237;k
Jiř&#237; Hern&#237;k

Reputation: 2477

Other option is to set the timeout for whole database which is what I am doing. You can do it in configuration, command line, mongo shell, or even C#.

see here: https://jira.mongodb.org/browse/SERVER-8188

This is the solution I am using currently in my init class

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

More information could be find here: https://docs.mongodb.com/v3.0/reference/parameters/#param.cursorTimeoutMillis

Upvotes: 0

I changed my code to this

 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);

It Works!!

Upvotes: 2

Related Questions