How to Fix MongoDB Error 136 (CappedPositionLost)
You get mongodb error 136 when a cursor loses its place in a capped collection. To fix this CappedPositionLost failure, restart the cursor or increase the capped collection size. Capped collections store fixed amounts of data and overwrite the oldest records when they fill up. If a reading client pauses for too long, new writes overwrite the exact document the cursor points to. The database engine then stops the query and returns this specific error code. What causes mongodb error 136 in prod
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 get mongodb error 136 when a cursor loses its place in a capped collection. To fix this CappedPositionLost failure, restart the cursor or increase the capped collection size.
Capped collections store fixed amounts of data and overwrite the oldest records when they fill up. If a reading client pauses for too long, new writes overwrite the exact document the cursor points to. The database engine then stops the query and returns this specific error code.
What causes mongodb error 136 in production
The official MongoDB error spec defines code 136 as CappedPositionLost. This failure happens inside a capped collection when the storage engine deletes the document at the current cursor position. Because capped collections work like circular buffers, high write volume pushes out older records fast.
You will see this most often on the replication oplog or on custom event queues. A secondary node or change stream reader might fall behind during a sudden traffic spike. When the reader asks for the next batch of records, its saved position no longer exists on disk.
Slow client processing also triggers this issue during normal write loads. If an app takes too long to process a batch, the write head laps the read cursor. Network drops and long garbage collection pauses on the client side make this lap much more likely.
How to diagnose and fix CappedPositionLost safely
Start your check by looking at collection stats and write rates on the database. You do not need write access to run these checks in production. MongoDB grants access through role-based rules that apply to the database where you define them. A role can grant access down to a single collection.
Assign your team the built-in read role to check collection health safely. The read role grants read access to all non-system collections in its database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not permit insert, update, remove, or dropCollection actions, which keeps production data safe during checks.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Once you check the collection size with collStats, compare it against your peak write volume. Notice how fast the capped collection rolls over during busy hours. If the rollover window is shorter than your longest client pause, the collection is too small.
To fix the error right away in your app, catch code 136 and open a fresh cursor. Your app should log the gap and start reading from the newest available record. For a lasting fix, resize the capped collection or oplog so it holds more hours of write history.
How to prevent cursor position loss over time
You can prevent future cursor drops by speeding up how your client reads batches. Keep your batch processing logic light so the cursor asks for new data right away. Separate heavy data work from the thread that reads the capped collection.
When engineers inspect capped collections to debug these stalls, protect sensitive fields in the query output. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results. This lets your team inspect broken documents and fix cursor bugs while keeping private records safe.
If you must change collection settings or add indexes to speed up reads, use the readWrite role temporarily. The readWrite role includes everything in the read role plus data modification actions. Those actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Remove this write role once your fix is in place.
Add command guardrails and field masking beside MongoDB
Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your MongoDB. For every command an agent or human sends in production, hoop.dev decides to pass, mask, block, or escalate it for review.
Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run or hold risky calls for approval, and data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.
Frequently asked questions
What does CappedPositionLost mean in MongoDB?
CappedPositionLost is the official name for error code 136 in MongoDB. It means a capped collection rolled over and deleted the document where a query cursor was resting.
Can a slow query cause mongodb error 136?
Yes, a slow query or slow client loop often causes this error. When the reader moves slower than incoming writes, new data overwrites the current cursor position.
What role do I need to check capped collection size?
You only need the built-in read role on the target database. This role allows the collStats and dbStats actions without granting permission to change or delete data.
Related guides
In this post