Class: GridFSBucketReadStream

GridFSBucketReadStream

new GridFSBucketReadStream(chunks, files, readPreference, filter, options){GridFSBucketReadStream}

A readable stream that enables you to read buffers from GridFS.

Do not instantiate this class directly. Use openDownloadStream() instead.

Name Type Default Description
chunks Collection

Handle for chunks collection

files Collection

Handle for files collection

readPreference Object

The read preference to use

filter Object

The query to use to find the file document

options Object null optional

Optional settings.

Name Type Default Description
sort Number null optional

Optional sort for the file find query

skip Number null optional

Optional skip for the file find query

start Number null optional

Optional 0-based offset in bytes to start streaming from

end Number null optional

Optional 0-based offset in bytes to stop streaming before

Fires:
Returns:
GridFSBucketReadStream instance.

Methods

Reads from the cursor and pushes to the stream.

Marks this stream as aborted (will never push another data event)
and kills the underlying cursor. Will emit the 'end' event, and then
the 'close' event once the cursor is successfully killed.

Name Type Description
callback GridFSBucket~errorCallback optional

called when the cursor is successfully closed or an error occurred.

Fires:
  • GridFSBucketWriteStream#event:close
  • GridFSBucketWriteStream#event:end
Example
// Calling abort() on a GridFSBucketReadStream

var MongoClient = require('mongodb').MongoClient,
  test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
  var bucket = new GridFSBucket(db,
    { bucketName: 'gridfsdestroy', chunkSizeBytes: 10 });
  var readStream = fs.createReadStream('./LICENSE');
  var uploadStream = bucket.openUploadStream('test.dat');

  var id = uploadStream.id;

  // Wait for stream to finish
  uploadStream.once('finish', function() {
    var id = uploadStream.id;
    var downloadStream = bucket.openDownloadStream(id);
    var done = {};
    downloadStream.on('data', function() {
      test.ok(false);
    });
    downloadStream.on('error', function() {
      test.ok(false);
    });
    downloadStream.on('end', function() {
      test.equal(downloadStream.s.cursor, null);
      if (done.close) {
      }
      done.end = true;
    });
    downloadStream.on('close', function() {
      if (done.end) {
      }
      done.close = true;
    });
    downloadStream.abort(function(error) {
      test.equal(error, null);
    });
  });

  readStream.pipe(uploadStream);

Sets the 0-based offset in bytes to start streaming from. Throws
an error if this stream has entered flowing mode
(e.g. if you've already called on('data'))

Name Type Description
end Number

Offset in bytes to stop reading at

Sets the 0-based offset in bytes to start streaming from. Throws
an error if this stream has entered flowing mode
(e.g. if you've already called on('data'))

Name Type Description
start Number

Offset in bytes to start reading at

Events

Fired when the stream is exhausted and the underlying cursor is killed

Type:
  • object

Emitted when a chunk of data is available to be consumed.

Type:
  • object

Fired when the stream is exhausted (no more data events).

Type:
  • object

An error occurred

Type:
  • Error

Fires when the stream loaded the file document corresponding to the
provided id.

Type:
  • object
comments powered by Disqus