6

I have quite an intensive operation that has a MongoCursor run in a loop for a few hours (on a vb.net app running via the c# driver. I'm not too sure what causes it but I run into an exception after a while

Cursor not found

This could be because of a cursor timeout, perhaps? Is there a way I can stop it happening? If its a timeout issue how do I place a longer timeout?

Tarang
  • 75,157
  • 39
  • 215
  • 276

3 Answers3

17

You can disable the cursor's timeout in the C# driver by calling:

cursor.SetFlags(QueryFlags.NoCursorTimeout);

Otherwise it will be closed after 10 minutes of inactivity.

Reference

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 2
    If you don't exhaust the cursor to the last element, this will cause a memory leak in mongo. – Nashenas Nov 30 '15 at 18:19
  • You can also change the batch size (as per http://stackoverflow.com/a/24200795/420867 - it's a python question but the same should be possible with the C# mongo driver) – drevicko Feb 07 '16 at 17:31
2

Further clarifying JohnnyHK's answer, this is the syntax:

MongoCursor<BsonDocument> cursor = myCollection
                                   .Find(query)
                                   .SetSortOrder(SortBy.Ascending("TrackingNumber"))
                                   .SetFlags(QueryFlags.NoCursorTimeout);
usmanc
  • 41
  • 2
1

I'm using MongoDB.Driver version 2.4.4 and IFindFluent is not containing SetFlags method. using this instead:

cursor.Options.NoCursorTimeout = true;
thirdDeveloper
  • 855
  • 1
  • 10
  • 30
  • 1
    What would happen if NoCursorTimeout is set to true and u have exhausted the cursor ? Would there be any memory leak or performance issues? – suresh rajput Mar 30 '21 at 05:07