Skip to main content
 首页 » 编程设计

mongodb之使用带有 NoCursorTimeout 的 Tailable 游标时出现“找不到游标”错误

2024年02月23日46EasonJim

我创建了一个默认大小为 50M 的上限集合。最近,我注意到当上限集合存储大小超过 50M 时,我会收到 Cursor not found 错误。我不确定是什么原因导致此问题:以前当上限集合大小小于默认最大大小时,我从未收到此错误。

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 find 异常(偶尔,但并不总是抛出。我的代码是否错误?

请您参考如下方法:

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

如果出现以下情况,可尾游标可能会失效或无效:

  1. 查询未返回任何匹配项。
  2. 光标返回集合“末尾”的文档,然后应用程序删除这些文档。

引用mongodb官网创建tailable游标( http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/ )

在我的应用程序中,当抛出“找不到光标”异常时,总是因为光标返回集合“末尾”的文档,然后应用程序删除了这些文档。