Database Reference
In-Depth Information
These are the minimum required attributes for every GridFS file. Most are self-
explanatory. You can see that this file is about 2 MB and is divided into chunks 256 KB
in size. You'll also notice an MD5 . The GridFS spec requires a checksum to ensure that
the stored file is the same as the original.
Each chunk stores the object ID of its file in a field called files_id . Thus you can
easily count the number of chunks this file uses:
> db.fs.chunks.count({"files_id" : ObjectId("4d606588238d3b4471000001")})
8
Given the chunk size and the total file size, eight chunks is exactly what you should
expect. The contents of the chunks themselves is easy to see, too. Like earlier, you'll
want to exclude the data to keep the output readable. This query returns the first of
the eight chunks, as indicated by the value of n :
> db.fs.chunks.findOne({files_id: ObjectId("4d606588238d3b4471000001")},
{data: 0})
{
"_id" : ObjectId("4d606588238d3b4471000002"),
"n" : 0,
"files_id" : ObjectId("4d606588238d3b4471000001")
}
Reading GridFS files is as easy as writing them. In the following example, you use
Grid#get to return an IO -like GridIO object representing the file. You can then stream
the GridFS file back to the file system. Here, you read 256 KB at a time to write a copy
of the original file:
image_io = @grid.get(file_id)
copy_filename = File.join(File.dirname(__FILE__), "canyon-copy.jpg")
copy = File.open(copy_filename, "w")
while !image_io.eof? do
copy.write(image_io.read(256 * 1024))
end
copy.close
You can then verify for yourself that both files are the same: 2
$ diff -s canyon.jpg canyon-copy.jpg
Files canyon.jpg and canyon-copy.jpg are identical
That's the basics of reading and writing GridFS files from a driver. The various GridFS
API s vary slightly, but with the foregoing examples and the basic knowledge of how
GridFS works, you should have no trouble making sense of your driver's docs.
2
This code assumes that you have the diff utility installed.
Search WWH ::




Custom Search