2013-04-20 79 views
1

我创建了一个带有50M默认大小的封顶集合。最近,我注意到当封顶的集合存储大小超过50M时,出现Cursor not found错误。我不确定是什么原因导致这种情况:在封顶收集大小小于默认最大大小之前,我从来没有收到过此错误。使用带有NoCursorTimeout的Tailable光标时出现'游标未找到'错误

if (this._cursor == null || this._cursor.IsDead) 
{     
    var cursor = this._queueCollection.Find(Query.GT("_id", this._lastId)) 
      .SetFlags(QueryFlags.AwaitData | 
      QueryFlags.TailableCursor | 
      QueryFlags.NoCursorTimeout) 
      .SetSortOrder(SortBy.Ascending("$natural")); 
    this._cursor =(MongoCursorEnumerator<QueueMessage<T>>)cursor.GetEnumerator(); 
} 

try 
{ 
    if (this._cursor.MoveNext()) 
     //do some things 
     return this._cursor.Current; 
    else 
    { 
     if (this._cursor.IsDead){ 
       this._cursor.Dispose(); 
       this._cursor=null; 
     } 
    } 
    return null; 
} 
catch{} 

this._cursor.MoveNext()将抛出一个异常cursor not found(偶尔,并不总是抛出是我的代码错

回答

1

我找到了导致此错误的原因。

如果出现以下情况,Tailable游标可能会死亡或无效:

  1. 该查询返回不匹配。
  2. 游标在集合的“结尾”返回文档,然后应用程序删除这些文档。从MongoDB的官方网站

参考有关创建tailable光标(http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/

在我的应用程序,在没有找到一个'光标”异常被抛出它始终是因为游标在返回文档的“结束“,然后应用程序删除那些文件。

0

你可以使用foreach:?

var query1 = new QueryBuilder<Message>().GT(m => m.date, lastTransferredMessageDate); 
var result = messagesCollection.FindAs<Message>(query1).SetFlags(QueryFlags.NoCursorTimeout); 

foreach (var message in result) 
{ 
    // Your code 
}