Senior · MongoDB with Go
Cursors, cancellation and partial results
Why can a cursor loop return no error but still lose records?
Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.
Reveal a strong answer
Iteration can fail after some documents were returned. Check decoding errors and cursor.Err after the loop, and close the cursor with an appropriate bounded cleanup context. Empty results are not the same as an iteration failure.
Avoid All for unbounded results; stream bounded batches with projections and limits. Batch size affects transfer and client memory, while query limits affect total result count. They are not interchangeable.
Propagate cancellation and downstream backpressure. Decide whether partial output is acceptable and whether a resume token can safely restart from a stable sort key. Do not report success after silently ignoring the final error.
Common trap
The initial Find succeeding does not guarantee that every later network batch succeeds.
Try it
A cursor returns100 records and then the connection fails. What must the API report?
Worked solution
Report the iteration failure according to the response protocol. If streaming already began, use the documented in-stream error/resume mechanism; do not claim the entire export succeeded. Test failure after a batch as well as failure at Find.
Change the constraint
What happens if cursor cleanup uses an already-cancelled context and cannot finish its network cleanup?
How did you do?
Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.
Not marked. Progress stays on this browser, separate from course completion.
Primary references
Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.