Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-2486

Each Cursor.hasNext() call seems to consume an entry when used with a request limit

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.5.2
    • Component/s: None

      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(

      {i}

      );
      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)

      { console.error(err.stack); }

      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.

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            pierre.mallet@neo9.fr Pierre Mallet
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: