1

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?

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • If you put the new DateTime() into a variable first, then run the query does it work better? – Matt Jan 30 '14 at 22:17
  • how much time is some time? you could maybe reach a timeout on the cursor – i3arnon Jan 30 '14 at 22:30
  • @I3arnon The default timeout for a cursor is 10 minutes. Unless the `noTimeout` flag is set. – Pete Garafano Jan 30 '14 at 22:32
  • How can I change the timeout? the process takes more than 10 minutes – José Arthur Ortiz Antunes Jan 30 '14 at 22:33
  • @JoséArthurOrtizAntunes according to http://docs.mongodb.org/manual/core/cursors/, it is closed after 10 minutes of inactivity. Otherwise, there should be a setting somewhere in the driver for turning off cursor timeout, just remember that means the server won't clean up cursors, so you have to. – Pete Garafano Jan 30 '14 at 22:39
  • 1
    Please note that is **10 minutes of inactivity** so if the cursor is iterating it is not timing out. It might help to add more specific information on what is happening in the loop and the actual timings, rows iterated etc. – Neil Lunn Jan 31 '14 at 00:17
  • You can [disable the cursor timeout](http://stackoverflow.com/a/14056796/1259510), but that's really a last resort. – JohnnyHK Jan 31 '14 at 02:14
  • How can I set the timeout? 'cursor.setflag()' does not exist in linq – José Arthur Ortiz Antunes Jan 31 '14 at 11:20
  • Possible duplicate of [MongoDB C# Driver 'Cursor not found'](http://stackoverflow.com/questions/14053803/mongodb-c-sharp-driver-cursor-not-found) – Jiří Herník Oct 14 '16 at 12:57

2 Answers2

2

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!!

  • 1
    If you fail to exhaust the cursor, the server will never clean it up, and it will be a resource leak, where the resources won't be cleaned up until the mongod process is restarted. It is important to exhaust the cursor or find a way to do it without `NoCursorTimeout`. – Pete Garafano Jan 31 '14 at 13:48
0

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

Jiří Herník
  • 2,412
  • 1
  • 25
  • 26