public class GridFS extends Object
Modifier and Type | Field and Description |
---|---|
protected String |
_bucketName
Deprecated.
Please use
getBucketName() for access. |
protected DBCollection |
_chunkCollection
Deprecated.
Please use
getChunksCollection() for access. |
protected DB |
_db
Deprecated.
Please use
getDB() for access. |
protected DBCollection |
_filesCollection
Deprecated.
Please use
getFilesCollection() for access. |
static String |
DEFAULT_BUCKET
bucket to use for the collection namespaces
|
static int |
DEFAULT_CHUNKSIZE
file's chunk size
|
static long |
MAX_CHUNKSIZE
Deprecated.
You can calculate max chunkSize with
a similar formula
Mongo.getMaxBsonObjectSize() - 500*1000.
Please ensure that you left enough space for metadata (500kb is enough). |
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
protected GridFSDBFile |
_fix(Object o)
Deprecated.
This method is NOT a part of public API and will be dropped in 3.x versions.
|
GridFSInputFile |
createFile()
This method creates an empty
GridFSInputFile instance. |
GridFSInputFile |
createFile(byte[] data)
creates a file entry.
|
GridFSInputFile |
createFile(File f)
creates a file entry.
|
GridFSInputFile |
createFile(InputStream in)
creates a file entry.
|
GridFSInputFile |
createFile(InputStream in,
boolean closeStreamOnPersist)
creates a file entry.
|
GridFSInputFile |
createFile(InputStream in,
String filename)
creates a file entry.
|
GridFSInputFile |
createFile(InputStream in,
String filename,
boolean closeStreamOnPersist)
creates a file entry.
|
GridFSInputFile |
createFile(String filename) |
List<GridFSDBFile> |
find(DBObject query)
finds a list of files matching the given query
|
List<GridFSDBFile> |
find(DBObject query,
DBObject sort)
finds a list of files matching the given query
|
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(String filename,
DBObject sort)
finds a list of files matching the given filename
|
GridFSDBFile |
findOne(DBObject query)
finds one file 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
|
String |
getBucketName()
gets the bucket name used in the collection's namespace
|
protected DBCollection |
getChunksCollection()
Gets the
DBCollection in which the binary chunks are stored. |
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
|
DBCursor |
getFileList(DBObject query,
DBObject sort)
gets a filtered list of files stored in this gridfs, sorted by param sort
|
protected DBCollection |
getFilesCollection()
Gets the
DBCollection in which the file’s metadata is stored. |
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
|
public static final int DEFAULT_CHUNKSIZE
@Deprecated public static final long MAX_CHUNKSIZE
Mongo.getMaxBsonObjectSize()
- 500*1000.
Please ensure that you left enough space for metadata (500kb is enough).public static final String DEFAULT_BUCKET
@Deprecated protected final DB _db
getDB()
for access.@Deprecated protected final String _bucketName
getBucketName()
for access.@Deprecated protected final DBCollection _filesCollection
getFilesCollection()
for access.@Deprecated protected final DBCollection _chunkCollection
getChunksCollection()
for access.public GridFS(DB db)
db
- database to work withMongoException
WriteConcern
public GridFS(DB db, String bucket)
db
- database to work withbucket
- bucket to use in the given databaseMongoException
WriteConcern
public DBCursor getFileList()
public DBCursor getFileList(DBObject query)
query
- filter to applypublic DBCursor getFileList(DBObject query, DBObject sort)
query
- filter to applysort
- sorting to applypublic GridFSDBFile find(ObjectId id)
id
- MongoException
public GridFSDBFile findOne(ObjectId id)
id
- MongoException
public GridFSDBFile findOne(String filename)
filename
- MongoException
public GridFSDBFile findOne(DBObject query)
query
- MongoException
public List<GridFSDBFile> find(String filename)
filename
- MongoException
public List<GridFSDBFile> find(String filename, DBObject sort)
filename
- sort
- MongoException
public List<GridFSDBFile> find(DBObject query)
query
- MongoException
public List<GridFSDBFile> find(DBObject query, DBObject sort)
query
- sort
- MongoException
@Deprecated protected GridFSDBFile _fix(Object o)
public void remove(ObjectId id)
id
- MongoException
public void remove(String filename)
filename
- MongoException
public void remove(DBObject query)
query
- MongoException
public GridFSInputFile createFile(byte[] data)
data
- the file's datapublic GridFSInputFile createFile(File f) throws IOException
f
- the file objectIOException
public GridFSInputFile createFile(InputStream in)
in
- an inputstream containing the file's datapublic GridFSInputFile createFile(InputStream in, boolean closeStreamOnPersist)
in
- an inputstream containing the file's datacloseStreamOnPersist
- indicate the passed in input stream should be closed
once the data chunk persistedpublic GridFSInputFile createFile(InputStream in, String filename)
in
- an inputstream containing the file's datafilename
- the file name as stored in the dbpublic GridFSInputFile createFile(InputStream in, String filename, boolean closeStreamOnPersist)
in
- an inputstream containing the file's datafilename
- the file name as stored in the dbcloseStreamOnPersist
- indicate the passed in input stream should be closed
once the data chunk persistedpublic GridFSInputFile createFile(String filename)
filename
- the file name as stored in the dbGridFS#createFile()} on how to use this method
public GridFSInputFile createFile()
GridFSInputFile
instance. On this
instance an OutputStream
can be obtained using the
GridFSInputFile.getOutputStream()
method. You can still call
GridFSInputFile.setContentType(String)
and
GridFSInputFile.setFilename(String)
. The file will be completely
written and closed after calling the OutputStream.close()
method on the output stream.public String getBucketName()
public DB getDB()
protected DBCollection getFilesCollection()
DBCollection
in which the file’s metadata is stored.protected DBCollection getChunksCollection()
DBCollection
in which the binary chunks are stored.