Skip to content

QueryStream drops data if event is set late #1907

@seanodell

Description

@seanodell

I ran into an issue where QueryStream#on("data") misses records if the event is configured late. Below are steps to replicate:

First, populate a test database with a number of records to iterate over (execute in MongoDB shell):

test_db = db.getMongo().getDB("test");

test_db.test.remove();

for ( var index = 0; index < 30000; index++) {
  test_db.test.insert({
    index : index,
    value : Math.floor((Math.random() * 10000) + 1)
  });
}

Then, using QueryStream, iterate over all records:

var host = 'localhost';
var port = 27017;
var db = 'test';
var timeout = 200;

var mongoose = require('mongoose');

var conn = mongoose.createConnection('mongodb://' + host + ':' + port + '/' + db);

var schema = mongoose.Schema({
  index : Number,
  value : Number
});

var model = conn.model('Test', schema, 'test');

var query = model.find({});

var queryStream = query.stream();

var dataCount = 0;

setTimeout(function() {
  queryStream.on('close', function() {
    console.log("Data Count: " + dataCount);
  });

  queryStream.on('error', function(err) {
    console.log("ERROR: " + err);
  });

  queryStream.on('data', function(doc) {
    dataCount += 1;

    if (Math.floor(Math.random() * 100) == 50) {
      queryStream.pause();
      process.nextTick(function() {
        queryStream.resume();
      });
    }
  });
}, timeout);

If you set the timeout value to 0, you will probably get a count of 30000 (the actual number of records). Set it higher and the count drops, eventually down to zero if you set it to 1000ms or more.

It looks like the QueryStream is iterating through records in the background whether or not an event has been set up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions