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?

9
  • If you put the new DateTime() into a variable first, then run the query does it work better? Commented Jan 30, 2014 at 22:17
  • how much time is some time? you could maybe reach a timeout on the cursor Commented Jan 30, 2014 at 22:30
  • @I3arnon The default timeout for a cursor is 10 minutes. Unless the noTimeout flag is set. Commented Jan 30, 2014 at 22:32
  • How can I change the timeout? the process takes more than 10 minutes Commented Jan 30, 2014 at 22:33
  • 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. Commented Jan 31, 2014 at 0:17

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.