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

4.0 Cursor with .limit(1) returns null instead of doc

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Blocker - P1 Blocker - P1
    • None
    • Affects Version/s: 4.0.0
    • Component/s: Shell
    • Labels:
      None
    • Not Needed

      It seems setting a limit(1) on a cursor will cause it to return null on `next()` even if there is a document to return IF you call 'hasNext()' before calling next(). It will return true for `hasNext()`.

      Steps to recreate:

      $ node --experimental-repl-await
      >  const m = require('mongodb'); const c = await m.MongoClient.connect('mongodb://127.0.0.1:27017', { useUnifiedTopology: true }) // or wherever you have a db
      > await c.db('test').collection('coll').insertMany([{x: 1}, {x: 2}, {x: 3}])
      > const cursor = c.db('test').collection('coll').find()
      > const cursor2 = c.db('test').collection('coll').find().limit(1)
      > await cursor.hasNext() --> true
      > await cursor2.hasNext() --> true
      > await cursor.next() --> { x: 1 }
      > await cursor2.next() ------> THIS RETURNS NULL, IT SHOULD RETURN THE DOC
      

            Assignee:
            eric.adum@mongodb.com Eric Adum (Inactive)
            Reporter:
            anna.herlihy@mongodb.com Anna Herlihy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: