Class: GridFSBucket

GridFSBucket

new GridFSBucket(db, options)

Constructor for a streaming GridFS interface

Name Type Description
db Db

A db handle

options object optional

Optional settings.

Name Type Default Description
bucketName string "fs" optional

The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot.

chunkSizeBytes number 255 * 1024 optional

Number of bytes stored in each chunk. Defaults to 255KB

writeConcern object optional

Optional write concern to be passed to write operations, for instance { w: 1 }

readPreference object optional

Optional read preference to be passed to read operations

Fires:
  • GridFSBucketWriteStream#event:index

Extends

  • external:EventEmitter

Methods

delete(id, callback)

Deletes a file with the given id

Name Type Description
id ObjectId

The id of the file doc

callback GridFSBucket~errorCallback optional

Removes this bucket's files collection, followed by its chunks collection.

Name Type Description
callback GridFSBucket~errorCallback optional

find(filter, options){Cursor}

Convenience wrapper around find on the files collection

Name Type Description
filter Object
options Object optional

Optional settings for cursor

Name Type Default Description
batchSize number 1000 optional

The number of documents to return per batch. See find command documentation.

limit number optional

Optional limit for cursor

maxTimeMS number optional

Optional maxTimeMS for cursor

noCursorTimeout boolean optional

Optionally set cursor's noCursorTimeout flag

skip number optional

Optional skip for cursor

sort object optional

Optional sort for cursor

openDownloadStream(id, options){GridFSBucketReadStream}

Returns a readable stream (GridFSBucketReadStream) for streaming file
data from GridFS.

Name Type Description
id ObjectId

The id of the file doc

options Object optional

Optional settings.

Name Type Description
start Number optional

Optional 0-based offset in bytes to start streaming from

end Number optional

Optional 0-based offset in bytes to stop streaming before

openDownloadStreamByName(filename, options){GridFSBucketReadStream}

Returns a readable stream (GridFSBucketReadStream) for streaming the
file with the given name from GridFS. If there are multiple files with
the same name, this will stream the most recent file with the given name
(as determined by the uploadDate field). You can set the revision
option to change this behavior.

Name Type Description
filename String

The name of the file to stream

options Object optional

Optional settings

Name Type Default Description
revision number -1 optional

The revision number relative to the oldest file with the given filename. 0 gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the newest.

start Number optional

Optional 0-based offset in bytes to start streaming from

end Number optional

Optional 0-based offset in bytes to stop streaming before

openUploadStream(filename, options){GridFSBucketWriteStream}

Returns a writable stream (GridFSBucketWriteStream) for writing
buffers to GridFS. The stream's 'id' property contains the resulting
file's id.

Name Type Description
filename string

The value of the 'filename' key in the files doc

options object optional

Optional settings.

Name Type Default Description
chunkSizeBytes number optional

Optional overwrite this bucket's chunkSizeBytes for this file

metadata object optional

Optional object to store in the file document's metadata field

contentType string optional

Optional string to store in the file document's contentType field

aliases array optional

Optional array of strings to store in the file document's aliases field

disableMD5 boolean false optional

Deprecated If true, disables adding an md5 field to file data

openUploadStreamWithId(id, filename, options){GridFSBucketWriteStream}

Returns a writable stream (GridFSBucketWriteStream) for writing
buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting
file's id.

Name Type Description
id string | number | object

A custom id used to identify the file

filename string

The value of the 'filename' key in the files doc

options object optional

Optional settings.

Name Type Default Description
chunkSizeBytes number optional

Optional overwrite this bucket's chunkSizeBytes for this file

metadata object optional

Optional object to store in the file document's metadata field

contentType string optional

Optional string to store in the file document's contentType field

aliases array optional

Optional array of strings to store in the file document's aliases field

disableMD5 boolean false optional

Deprecated If true, disables adding an md5 field to file data

rename(id, filename, callback)

Renames the file with the given _id to the given string

Name Type Description
id ObjectId

the id of the file to rename

filename String

new name for the file

callback GridFSBucket~errorCallback optional

Type Definitions

errorCallback(error, result)

Callback format for all GridFSBucket methods that can accept a callback.

Name Type Description
error MongoError | undefined

If present, an error instance representing any errors that occurred

result *

If present, a returned result for the method

Events

When the first call to openUploadStream is made, the upload stream will
check to see if it needs to create the proper indexes on the chunks and
files collections. This event is fired either when 1) it determines that
no index creation is necessary, 2) when it successfully creates the
necessary indexes.

Type:
  • Error