-
Type:
Improvement
-
Resolution: Works as Designed
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 7.7.0
-
Component/s: Cursors
-
2
-
Not Needed
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Use Case
As a developer,
I want to be able to proactively trigger database fetch via cursor.hasNext() without awaiting the result.
So that I could minimize latency when doing multiple interactions over the network.
User Experience
- cursor.hasNext() should be idempotent irrespective of whether the result is awaited or not if the cursor is not consumed in between the invocations.
Current behavior
Non-deterministic.
- One time I was able to desync the command stream over the connection but not exactly on 7.7.0 (two hasNext() invocations triggered two find() operations over the same connection, the first response populated the cursor, the second - stayed in the socket and was read as the response of a different findOneAndUpdate operation)
- triggers multiple find() operations over different connections and eventually throws MongoExpiredSessionError: Cannot use a session that has ended
The code:
import {MongoClient, SeverityLevel} from 'mongodb'; import {setTimeout} from 'timers/promises';const collection = new MongoClient("mongodb://localhost:27017", { ignoreUndefined: true, mongodbLogComponentSeverities: { topology: SeverityLevel.INFORMATIONAL, serverSelection: SeverityLevel.INFORMATIONAL, connection: SeverityLevel.INFORMATIONAL, default: SeverityLevel.TRACE, }, }).db("test").collection("test");const cursor = collection.find(); for (let i = 0; i < 10; i++) { await setTimeout(1); void cursor.hasNext(); } await cursor.hasNext(); await cursor.close();const result = await collection.db.command({ping: 1}); console.log(result); await collection.db.client.close();
- is related to
-
NODE-7953 Document that cursors do not support overlapping hasNext() / next() / tryNext() calls
-
- Ready for Work
-