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

标签 mongodb mongodb-.net-driver

我创建了一个默认大小为 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/ )

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

关于mongodb - 使用带有 NoCursorTimeout 的 Tailable 游标时出现“找不到游标”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16121796/

相关文章:

c# - MongoDB - 在 C# 中映射 map-reduce 集合

node.js - 尝试从数据库集合(mongodb)获取消息历史记录

mongodb - mongodb 2dsphere 索引的内部结构是什么?

mongodb - 使用 mongo C# 驱动程序创建自定义 ID

c# - 使用 MongoDB C# 驱动程序在嵌套数组上使用过滤器生成器进行查询

mongodb - 使用 mongodb csharp 的索引提示

javascript - nodejs查询数据和代码组织

javascript - Mongoose 更新嵌套值

mongodb - mongodb slave 上的数据库与 master 不相同

c# - 如何计算C# Mongodb强类型驱动程序中单个属性的平均值