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

Cursor.hasNext() seems to consume first document when the cursor is piped in a writable stream

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

      Hi there, first of all, thanks for your amazing work : )

      We recently upgraded our application to use the 3.5.2 mongo driver and we experience some problems when piping our cursor into writable streams. 

      Here is a minimal example of reproduction 

      package.json
       

      { "dependencies": { "mongodb": "3.5.2" } }

      index.js

      const MongoClient = require('mongodb').MongoClient;
      const Transform = require('stream').Transform;
      const url = 'mongodb://localhost:27017'; 
      const client = new MongoClient(url, { useUnifiedTopology: true });
      (async () => {
          await client.connect();
          const db = client.db('testDb');
          const collection = db.collection('documents');
          await collection.drop();
          await collection.insertMany([ { a: 1 }, { a: 2 }, { a: 3 } ]); 
          const cursor = collection.find({}, { sort: { a:1}});
      
          cursor.hasNext(); // <= here
      
          cursor.pipe(new Transform({
               objectMode:true,
               transform: (chunk, encoding, next) => {
                   next(null, JSON.stringify(chunk));
               }
          }))
          .pipe(process.stdout);
      })();

      Version of node.js

      v10.15.0

      the output is : 

      {"_id":"5e442683c11e6609f16af231","a":2}{"_id":"5e442683c11e6609f16af232","a":3}

      If i remove the cursor.hasNext();

      the output is 

      {"_id":"5e4426b7d3c8f60d33560256","a":1}{"_id":"5e4426b7d3c8f60d33560257","a":2}{"_id":"5e4426b7d3c8f60d33560258","a":3}

      Thanks for your help, tell me if you need any other information 

       

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

              Created:
              Updated:
              Resolved: