Using nodejs highland in nodejs with mongodb>= 4

65 Views Asked by At

I'm trying to create a simple highland stream from a Mongo query

const connectionString = ...
const client = new MongoClient(connectionString);
const records = mongoClient
  .db('mydb')
  .collection('acollection')
  .find({});
const res = await hi(records).tap(_log).collect().toPromise(Promise);

function _log(record) {
  console.log(record);
  return record;
}

This code works perfectly with mongodb (the npm package) < 4.0.0, but with Mongo >= 4.0.0 it prints an undefined over and over until its heap runs out.

I'm running MongoDb in docker, version image mongo:4.2.10

1

There are 1 best solutions below

0
Dotan On

See this note from the mongodb 4.0.0 changelog

Stream API

The Cursor no longer extends Readable directly, it must be transformed into a stream by calling cursor.stream(), for example:

const stream = cursor.stream();
stream.on('data', data => console.log(data));
stream.on('end', () => client.close());

So the fix to the issue is to simply add .stream() at the end of the mongo query:

const connectionString = ...
const client = new MongoClient(connectionString);
const records = mongoClient
  .db('mydb')
  .collection('acollection')
  .find({})
  .stream(); // Change is here
const res = await hi(records).tap(_log).collect().toPromise(Promise);

function _log(record) {
  console.log(record);
  return record;
}