Database Reference
In-Depth Information
appendix C
Binary data and GridFS
For storing images, thumbnails, audio, and other binary files, many applications rely
on the file system only. Although file systems provide fast access to files, file system
storage can also can lead to organizational chaos. Consider that most file systems limit
the number of files per directory. If you have millions of files to keep track of, then
you need to devise a strategy for organizing files into multiple directories. Another dif-
ficulty involves metadata. Since the file metadata is still stored in a database, perform-
ing an accurate backup of the files and their metadata can be incredibly complicated.
For certain use cases, it may make sense to store files in the database itself because
it simplifies file organization and backup. In MongoDB, you can use the BSON binary
type to store any kind of binary data. This data type corresponds to the RDBMS BLOB
(binary large object) type, and it's the basis for two flavors of binary object storage pro-
vided by MongoDB.
The first uses one document per file and is best for smaller binary objects. If you
need to catalog a large number of thumbnails or MD5 s, then using single-document
binary storage can make life much easier. On the other hand, you might want to store
large images or audio files. In this case, GridFS, a Mongo DB API for storing binary
objects of any size, would be a better choice. In the next two sections, you'll see com-
plete examples of both storage techniques.
C.1
Simple binary storage
BSON includes a first-class type for binary data. You can use this type to store binary
objects directly inside MongoDB documents. The only limit on object size is the docu-
ment size limit itself, which is 16 MB as of MongoDB v2.0. Because large documents
like this can tax system resources, you're encouraged to use GridFS for any binary
objects you want to store that are larger than 1 MB .
We'll look at two reasonable uses of binary object storage in single documents.
First, you'll see how to store an image thumbnail. Then, you'll see how to store the
accompanying MD5.
C.1.1
Storing a thumbnail
Imagine you need to store a collection of image thumbnails. The code is straightfor-
ward. First, you get the image's filename, canyon-thumb.jpg , and then read the data
260
Search WWH ::




Custom Search