public class

GridFS

extends Object
java.lang.Object
   ↳ com.mongodb.gridfs.GridFS

Class Overview

Implementation of GridFS v1.0 GridFS 1.0 spec

Summary

Constants
String DEFAULT_BUCKET bucket to use for the collection namespaces
int DEFAULT_CHUNKSIZE file's chunk size
long MAX_CHUNKSIZE file's max chunk size
Fields
protected final String _bucketName
protected final DBCollection _chunkCollection
protected final DB _db
protected final DBCollection _filesCollection
Public Constructors
GridFS(DB db)
Creates a GridFS instance for the default bucket "fs" in the given database.
GridFS(DB db, String bucket)
Creates a GridFS instance for the specified bucket in the given database.
Public Methods
GridFSInputFile createFile(byte[] data)
creates a file entry.
GridFSInputFile createFile(InputStream in, boolean closeStreamOnPersist)
creates a file entry.
GridFSInputFile createFile(InputStream in)
creates a file entry.
GridFSInputFile createFile()
This method creates an empty GridFSInputFile instance.
GridFSInputFile createFile(File f)
creates a file entry.
GridFSInputFile createFile(InputStream in, String filename, boolean closeStreamOnPersist)
creates a file entry.
GridFSInputFile createFile(String filename)
GridFSInputFile createFile(InputStream in, String filename)
creates a file entry.
GridFSDBFile find(ObjectId id)
finds one file matching the given id.
List<GridFSDBFile> find(String filename)
finds a list of files matching the given filename
List<GridFSDBFile> find(DBObject query)
finds a list of files matching the given query
GridFSDBFile findOne(ObjectId id)
finds one file matching the given id.
GridFSDBFile findOne(String filename)
finds one file matching the given filename
GridFSDBFile findOne(DBObject query)
finds one file matching the given query
String getBucketName()
gets the bucket name used in the collection's namespace
DB getDB()
gets the db used
DBCursor getFileList()
gets the list of files stored in this gridfs, sorted by filename
DBCursor getFileList(DBObject query)
gets a filtered list of files stored in this gridfs, sorted by filename
void remove(DBObject query)
removes all files matching the given query
void remove(ObjectId id)
removes the file matching the given id
void remove(String filename)
removes all files matching the given filename
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String DEFAULT_BUCKET

bucket to use for the collection namespaces

Constant Value: "fs"

public static final int DEFAULT_CHUNKSIZE

file's chunk size

Constant Value: 262144 (0x00040000)

public static final long MAX_CHUNKSIZE

file's max chunk size

Constant Value: 3500000 (0x00000000003567e0)

Fields

protected final String _bucketName

protected final DBCollection _chunkCollection

protected final DB _db

protected final DBCollection _filesCollection

Public Constructors

public GridFS (DB db)

Creates a GridFS instance for the default bucket "fs" in the given database. Set the preferred WriteConcern on the give DB with DB.setWriteConcern

Parameters
db database to work with
See Also

public GridFS (DB db, String bucket)

Creates a GridFS instance for the specified bucket in the given database. Set the preferred WriteConcern on the give DB with DB.setWriteConcern

Parameters
db database to work with
bucket bucket to use in the given database
See Also

Public Methods

public GridFSInputFile createFile (byte[] data)

creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters
data the file's data

public GridFSInputFile createFile (InputStream in, boolean closeStreamOnPersist)

creates a file entry. after calling this method, you have to call save() on the GridFSInputFile file

Parameters
in an inputstream containing the file's data
closeStreamOnPersist indicate the passed in input stream should be closed once the data chunk persisted

public GridFSInputFile createFile (InputStream in)

creates a file entry. after calling this method, you have to call save() on the GridFSInputFile file

Parameters
in an inputstream containing the file's data

public GridFSInputFile createFile ()

This method creates an empty GridFSInputFile instance. On this instance an OutputStream can be obtained using the getOutputStream() method. You can still call setContentType(String) and setFilename(String). The file will be completely written and closed after calling the close() method on the output stream.

Returns
  • GridFS file handle instance.

public GridFSInputFile createFile (File f)

creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file@return

Parameters
f the file object
Throws
IOException

public GridFSInputFile createFile (InputStream in, String filename, boolean closeStreamOnPersist)

creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters
in an inputstream containing the file's data
filename the file name as stored in the db
closeStreamOnPersist indicate the passed in input stream should be closed once the data chunk persisted

public GridFSInputFile createFile (String filename)

Parameters
filename the file name as stored in the db
See Also
  • {@link GridFS#createFile()} on how to use this method

public GridFSInputFile createFile (InputStream in, String filename)

creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters
in an inputstream containing the file's data
filename the file name as stored in the db

public GridFSDBFile find (ObjectId id)

finds one file matching the given id. Equivalent to findOne(id)

public List<GridFSDBFile> find (String filename)

finds a list of files matching the given filename

public List<GridFSDBFile> find (DBObject query)

finds a list of files matching the given query

public GridFSDBFile findOne (ObjectId id)

finds one file matching the given id.

public GridFSDBFile findOne (String filename)

finds one file matching the given filename

public GridFSDBFile findOne (DBObject query)

finds one file matching the given query

public String getBucketName ()

gets the bucket name used in the collection's namespace

public DB getDB ()

gets the db used

public DBCursor getFileList ()

gets the list of files stored in this gridfs, sorted by filename

Returns
  • cursor of file objects

public DBCursor getFileList (DBObject query)

gets a filtered list of files stored in this gridfs, sorted by filename

Parameters
query filter to apply
Returns
  • cursor of file objects

public void remove (DBObject query)

removes all files matching the given query

public void remove (ObjectId id)

removes the file matching the given id

public void remove (String filename)

removes all files matching the given filename