Using cursor for iterating results I get "MongoDB.Driver.MongoCommandException: Command getMore failed: Cursor not found"

2,532 views
Skip to first unread message

Marcus Haßmann

unread,
Dec 31, 2015, 7:51:50 PM12/31/15
to mongodb-user
I get the following exception:
MongoDB.Driver.MongoCommandException: Command getMore failed: Cursor not found, cursor id: 16494659487.
   at
MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at
MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ClusterableServer.ServerChannel.ExecuteProtocol[TResult](IWireProtocol`
1 protocol, CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Servers.ClusterableServer.ServerChannel.Command[TResult](DatabaseNamespace databaseNamespace, BsonDocument command, IElementNameValidator commandValidator, Func`1 responseHandling, Boolean slaveOk, IBsonSerializer`1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Operations.AsyncCursor`1.ExecuteGetMoreCommand(IChannelHandle channel, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AsyncCursor`
1.GetNextBatch(CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Operations.AsyncCursor`1.MoveNext(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AsyncCursorEnumerator`
1.MoveNext()


I am using the newest NuGet packages:
  <package id="MongoDB.Bson" version="2.2.0" targetFramework="net452" />
 
<package id="MongoDB.Driver" version="2.2.0" targetFramework="net452" />
 
<package id="MongoDB.Driver.Core" version="2.2.0" targetFramework="net452" />


This is the executed code:
try
{
 
var mylist = database.GetCollection<MyClass>("MyCollectionName");
 
var cursor = mylist.Find(x => x.Subdocument.Any(y => y.BoolValue == false)).ToCursor();
 
foreach (var myObject in cursor.ToEnumerable())
 
{
 
Console.WriteLine();
 
Console.WriteLine("myObject-ID: " + myObject.myObjectId);


 
var myObjectChanged = false;
 
var subDocs = myObject.Subdocument.Where(x => x.BoolValue == false).ToList();
 
var subDocCount = subDocs.Count;
 
for (var i = 0; i < subDocCount; i++)
 
{
 
var myObject = subDocs[i];
 
...
 
if (...)
 
{
 myObjectChanged
= true;
 
}
 
}


 
if (myObjectChanged)
 
{
 mylist
.ReplaceOne(x => x.myObjectId == myObject.myObjectId, myObject);
 
}
 
}
}
catch (Exception ex)
{
 
Console.WriteLine(ex);
}

Robert Stam

unread,
Dec 31, 2015, 10:52:28 PM12/31/15
to mongod...@googlegroups.com
The most common reason for seeing a cursor not found error is that the server kills any cursors that have been idle for 10 minutes. The results of a query are returned from the server in batches. If it takes more than 10 minutes for an application to process a batch you will get this error when the driver attempts to get the next batch.

A less common reason for seeing a cursor not found error is when the mongos'es in a sharded cluster are deployed behind a load balancer. In this case the getMore command might be sent to a different mongos instance than the initial query. When this happens, the mongos instance receiving the getMore command can't find that cursor.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/589b4551-66d9-4d6c-b6e3-f49c4249720b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Haßmann

unread,
Jan 3, 2016, 11:56:58 AM1/3/16
to mongodb-user
Very nice answer, thank you very much!

With following changes, no MongoDB.Driver.MongoCommandException occured.

var findOptions = new FindOptions {NoCursorTimeout = true};
var count = myList.Find(filter).Count();
using (var cursor = myList.Find(filter, findOptions).ToCursor())
{
 
while (cursor.MoveNext())
 
{
 
var index = 0;
 
foreach (var myObject in cursor.Current)
 
{

Robert Stam

unread,
Jan 3, 2016, 12:13:11 PM1/3/16
to mongod...@googlegroups.com
Thanks for letting me know that adding NoCursorTimeout worked for you!

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages