How to Fix MongoDB Error 43 (CursorNotFound)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 43 when a query takes too long and its cursor expires. To fix this CursorNotFound issue, pull smaller batches or speed up your read logic. Database servers keep open query pointers in memory while your app reads through results. If your app waits too long between requests, the server drops that pointer to save memory. The next batch request fails with a CursorInvalidatedError response. What causes mongodb error 43 in production The server assigns code 43 with the name C

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You see mongodb error 43 when a query takes too long and its cursor expires. To fix this CursorNotFound issue, pull smaller batches or speed up your read logic.

Database servers keep open query pointers in memory while your app reads through results. If your app waits too long between requests, the server drops that pointer to save memory. The next batch request fails with a CursorInvalidatedError response.

What causes mongodb error 43 in production

The server assigns code 43 with the name CursorNotFound under the CursorInvalidatedError category. Several runtime conditions cause the database engine to clear an active cursor before completion.

Idle time is the most common trigger in day-to-day database operations. An app fetches the first batch of documents and then runs slow processing logic. While the app works on that batch, the server waits for the next request. Once the server wait timer runs out, the engine removes the cursor state.

Session mismatches also trigger this error in distributed database clusters. When a client sends a cursor tied to a specific session, both must match. If the session expires or routes to a different node, the cursor fails. Primary node stepdowns or collection drops during a scan also clear open cursors.

Large unindexed scans during manual data checks often hit this limit too. Engineers running ad-hoc find queries over huge collections often pause to inspect results. When they try to fetch the next page, the server returns code 43.

How to diagnose and fix CursorNotFound safely

Start by checking your app logs and database logs around the failure time. Look for slow processing steps that happen inside your cursor loop. Move heavy network calls or file writes outside the database read loop. This lets the client pull all batches fast before the cursor expires.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Next, lower the batch size in your query settings. Smaller batches mean the app processes fewer records before asking the server for more. Because the client talks to the server more often, the cursor stays active.

Avoid turning off cursor timeouts in your query options to bypass the error. Leaving cursors open forever leaks server memory when an app crashes mid-query. Instead, use indexed fields like an ID or timestamp to paginate through collections. Each page runs as a fresh, fast query that does not need long-lived state.

When engineers debug this issue in production, keep database permissions tight. MongoDB grants access through role-based authorization down to the collection level. Give engineers a read role on the target database for their investigation. The read role grants find, listCollections, listIndexes, dbStats, and collStats on non-system collections. It blocks insert, update, remove, and dropCollection actions so nobody changes live data.

Preventing cursor failures during live data checks

Ad-hoc production queries often cause cursor drops and load spikes. An engineer might pull millions of documents over a slow client link. If that query touches sensitive tables, it also exposes private user records.

Teams can prevent both problems by putting guardrails on live database queries. Enforce strict query limits and short timeouts on human sessions to stop runaway scans. Apply data masking to rewrite matched fields like emails, phone numbers, or card numbers in results. This lets engineers inspect query behavior and fix code 43 errors safely.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What does CursorNotFound mean in MongoDB?

CursorNotFound is the official name for error code 43. It means the server deleted the query pointer before the client finished reading all batches.

Should I disable cursor timeouts to fix mongodb error 43?

No, disabling timeouts is risky in production databases. If a client crashes, the cursor stays in memory until the server restarts. Use smaller batch sizes or range-based pagination instead.

What role does an engineer need to check indexes for slow cursors?

The built-in read role is enough to check query plans and indexes. It allows find and listIndexes actions without granting write or drop permissions.

The query runs. The emails stay hidden.