- Newest
- Most votes
- Most comments
While official documentation for Code 40 in DocumentDB is sparse, this behavior (especially on read-only findOne + populate patterns) strongly suggests an internal Read-View or History Store conflict within the shared storage volume.
- Internal Execution Failure: In DocumentDB's architecture, Code 40 often surfaces when the query processor cannot maintain a consistent snapshot of the data. If the storage layer's garbage collection (purging old versions) is too aggressive or struggling after a load spike, the reader may lose the "version" of the document it is trying to fetch.
- Empty
errorLabels: The absence of labels likeTransientTransactionErrorindicates that the engine does not classify this as a standard retryable network/failover event, but rather a hard execution break at the storage level. - Escalation Pattern: The fact that the rate is growing suggests a "fragmentation" or "backlog" issue in the underlying storage history that hasn't cleared since the May incident.
I would try the following actions:
- Monitor
LowOldestReadTimestamp: Check CloudWatch for correlations between this metric and your error spikes. - Manual Retry Logic: Since the driver isn't getting the labels, implement a manual client-side retry specifically for
code: 40. Given your 15–300ms error duration, a second attempt will likely succeed. - Audit Logs: Enable DML auditing briefly to see if the server-side logs provide more context than the driver’s raw payload.
Hope that helps!
Based on the available documentation, I cannot find specific information about error code 40 in DocumentDB 5.0 or the "Error in query execution" message you're encountering. The sources provided do not contain details about this particular error code or its triggers in the DocumentDB context.
However, I can address some aspects of your situation based on documented behaviors:
Regarding retryable operations and errorLabels:
Amazon DocumentDB does not currently support retryable writes. When using MongoDB 4.2+ compatible drivers (which enable retryable writes by default), you need to explicitly disable this feature in your connection string or client constructor. You've already done this with retryWrites=false in your connection string, which is correct.
The empty errorLabels array you're seeing means the error is not classified as a transient transaction error or retryable write error by DocumentDB, which explains why the driver doesn't auto-retry. This behavior aligns with DocumentDB's current limitations around retry semantics.
Regarding read preferences and failover:
There is a documented issue with certain Java Mongo drivers where read preferences may not be honored correctly, and clients can enter a bad state after failover events. While your setup uses Node.js, it's worth noting that driver behavior during cluster state changes can sometimes produce unexpected results. Your use of secondaryPreferred with the cluster endpoint should generally work correctly, but driver-level connection handling during internal cluster events could potentially contribute to intermittent errors.
What I recommend:
- This specific error code and behavior is not documented in the available public documentation, so you should open a support case with AWS to get authoritative guidance on what code 40 represents in DocumentDB 5.0
- Request cluster-level diagnostic logs that might correlate with these error timestamps
- Ask AWS Support specifically about any internal maintenance operations, compaction cycles, or replica synchronization behaviors that might trigger query cancellations
The escalating rate pattern you're observing and its correlation with the load incident suggests this may be related to an internal cluster state that hasn't fully recovered or stabilized, but without access to internal DocumentDB diagnostics, I cannot provide a definitive explanation.
Sources
Functional differences: Amazon DocumentDB and MongoDB - Amazon DocumentDB
Troubleshooting connectivity issues - Amazon DocumentDB
Relevant content
- asked 6 years ago
- asked 2 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 8 months ago

Update — fully resolved. Turned out the root cause was on our side: a hot read path on a 12.6k-doc collection had no usable index for its filter, so every call was a COLLSCAN running 50–300ms. On DocDB 4.0 that was just slow; after our migration to 5.0 in late April, those slow in-flight reads started getting cancelled internally and surfaced to the client as
code: 40 / "Error in query execution". The cluster itself was healthy by every external metric, which is why nothing showed up in CPU/MVCC/GC. We added the missing compound index online (background:true, ~2s for 12.6k docs) and the error rate went from a steady ~290/day, escalating, straight to zero — 10+ hours later including peak hours, no occurrences. Reader CPU also dropped ~35% instantly. Thanks again for pushing us toward the manual retry and the audit/profiler ideas — even though the audit/profiler didn't capture the failures themselves, the profiler at 50ms is what surfaced the COLLSCAN-heavy pattern that pointed to the missing index. Really appreciate the help.