2

Not sure of the proper way to pipe query results using MongoDB Node.js Drive 3.0. According to the docs, Cursor::pipe and AggregateCursor::pipe receive a Writeable streams so it seems like you should be able to do something like this:

collection.find(query).pipe(process.stdout);
collection.aggregate(query).pipe(process.stdout);

But it turns out to throw an error:

TypeError: Invalid data, chunk must be a string or buffer, not object

This works but it feels a little cumbersome to me doesn't lend itself to pipe chaining.

collection.find(query)
    .on('data', function(chunk) {
        buffer = Buffer.from(JSON.stringify(chunk))
        process.stdout.write( buffer );
    });


collection.aggregate(query)
    .on('data', function(chunk) {
        buffer = Buffer.from(JSON.stringify(chunk))
        process.stdout.write( buffer );
    });

Is there a way to do a standard pipe chain with with the MongoDB Driver for Node.js?

jwerre
  • 9,179
  • 9
  • 60
  • 69

1 Answers1

3

It appears the error was the result of my trying to pipe a stream in objectMode to a stream not in objectMode. Here is a solution:

collection.find(query)
    .stream({
        transform: function(chunk) {
            return JSON.stringify(chunk);
        }
    }).pipe(process.stdout);

collection.aggregate(query)
    .stream({
        transform: function(chunk) {
            return JSON.stringify(chunk);
        }
    }).pipe(process.stdout);

You could also extend stream.Writable and set it to objectMode, but the above seems like the simplest solution.

jwerre
  • 9,179
  • 9
  • 60
  • 69