-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.5.2
-
Component/s: None
-
Empty show more show less
We've had to pin our version to 3.5.3 as this code change changed behaviour.
Simple sample recreate of the change in behavior:
async function run() {
const MongoClient = require('mongodb').MongoClient;
const client = await MongoClient.connect(process.env.MONGODB_URL);
const db = client.db('354regr');
const collection = db.collection('numbers');
try {
await collection.deleteMany({});
for (let i = 0; i < 10; i++) await collection.insertOne(
);
let cursor = await collection.find({});
cursor.limit(5);
while (await cursor.hasNext()) {
const doc = await cursor.next();
console.log(`Doc: ${JSON.stringify(doc)}`);
}
}
catch(err)
client.close();
}
run()
Output with mongodb@3.5.3 (or mongodb@3.5.2 and previous) - see the limit of 5 records returned as expected:
Doc:
{"_id":"5e6012c0bab232836f58f35b","i":0}Doc:
{"_id":"5e6012c0bab232836f58f35c","i":1}Doc:
{"_id":"5e6012c0bab232836f58f35d","i":2}Doc:
{"_id":"5e6012c0bab232836f58f35e","i":3}Doc:
{"_id":"5e6012c0bab232836f58f35f","i":4}Output with mongodb@3.5.4:
Doc:
{"_id":"5e601344f32806839a905a99","i":0}Doc:
{"_id":"5e601344f32806839a905a9a","i":1}Doc: null
MongoError: Cursor is closed
at Function.create (/private/tmp/node_modules/mongodb/lib/core/error.js:43:12)
at Cursor.hasNext (/private/tmp/node_modules/mongodb/lib/cursor.js:197:24)
at run (/private/tmp/t/index.js:12:25)
at process._tickCallback (internal/process/next_tick.js:68:7)
See that each time hasNext is called, it seems to be consuming one of the limit entries, resulting in a null doc being returned on the third call to next() and the cursor being closed.