CursorNotFound data resource_error ai_generated true

MongoDB CursorNotFoundException: cursor not found on server after timeout

ID: data/mongodb-cursor-timeout-exceeded

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
MongoDB 5.0 active
MongoDB 6.0 active
MongoDB 7.0 active

Root Cause

MongoDB server-side cursors have a default idle timeout of 10 minutes; if the client pauses iteration beyond this time, the server garbage-collects the cursor, causing the next getMore call to fail.

generic

中文

MongoDB服务端游标的默认空闲超时时间为10分钟;如果客户端暂停迭代超过此时间,服务器会回收游标,导致下一次getMore调用失败。

Official Documentation

https://www.mongodb.com/docs/manual/reference/method/cursor.noCursorTimeout/

Workarounds

  1. 95% success Enable noCursorTimeout on the cursor to prevent server-side garbage collection, but ensure you manually close the cursor to avoid resource leaks.
    Enable noCursorTimeout on the cursor to prevent server-side garbage collection, but ensure you manually close the cursor to avoid resource leaks.
  2. 78% success Re-query with a filter on a timestamp field or use aggregation with $match to restart iteration from the last processed document.
    Re-query with a filter on a timestamp field or use aggregation with $match to restart iteration from the last processed document.

中文步骤

  1. Enable noCursorTimeout on the cursor to prevent server-side garbage collection, but ensure you manually close the cursor to avoid resource leaks.
  2. Re-query with a filter on a timestamp field or use aggregation with $match to restart iteration from the last processed document.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The cursor still expires on the server regardless of batch size if no activity occurs.

  2. 85% fail

    maxTimeMS controls query runtime, not cursor lifetime.