Class: GridFSBucket

GridFSBucket

new GridFSBucket(db, options){GridFSBucket}

Constructor for a streaming GridFS interface

Name Type Default Description
db Db

A db handle

options object null 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 null optional

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

readPreference object null optional

Optional read preference to be passed to read operations

Fires:
  • GridFSBucketWriteStream#event:index

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 Default Description
filter Object
options Object null optional

Optional settings for cursor

Name Type Default Description
batchSize number null optional

Optional batch size for cursor

limit number null optional

Optional limit for cursor

maxTimeMS number null optional

Optional maxTimeMS for cursor

noCursorTimeout boolean null optional

Optionally set cursor's noCursorTimeout flag

skip number null optional

Optional skip for cursor

sort object null optional

Optional sort for cursor

openDownloadStream(id, options){GridFSBucketReadStream}

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

Name Type Default Description
id ObjectId

The id of the file doc

options Object null optional

Optional settings.

Name Type Default Description
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

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 Default Description
filename String

The name of the file to stream

options Object null 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 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

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 Default Description
filename string

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

options object null optional

Optional settings.

Name Type Default Description
chunkSizeBytes number null optional

Optional overwrite this bucket's chunkSizeBytes for this file

metadata object null optional

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

contentType string null optional

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

aliases array null optional

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

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 Default 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 null optional

Optional settings.

Name Type Default Description
chunkSizeBytes number null optional

Optional overwrite this bucket's chunkSizeBytes for this file

metadata object null optional

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

contentType string null optional

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

aliases array null optional

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

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)

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

Name Type Description
error MongoError

An error instance representing any errors that occurred

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