Database Reference
In-Depth Information
canyon.jpg
1MB
Grid#put
GridFS Interface
(e.g., driver or mongofiles)
1. Write to chunks collection (fs.chunks)
Divide into chunks
les_id: 5, n: 0, data: BinData(0, "(256KB binary data)" }
les_id: 5, n: 1, data: BinData(0, "(256KB binary data)" }
les_id: 5, n: 2, data: BinData(0, "(256KB binary data)" }
les_id: 5, n: 3, data: BinData(0, "(256KB binary data)" }
W
le metadata
2. W
les)
lename: "canyon.jpg" }
Figure C.1
Storing a file with GridFS
The method returns the file's unique object ID :
@con = Mongo::Connection.new
@db
= @con["images"]
@grid = Mongo::Grid.new(@db)
filename = File.join(File.dirname(__FILE__), "canyon.jpg")
file = File.open(filename, "r")
file_id = @grid.put(file, :filename => "canyon.jpg")
As stated, GridFS uses two collections for storing file data. The first, normally called
fs.files , keeps each file's metadata. The second collection, fs.chunks , stores one or
more chunks of binary data for each file. Let's briefly examine these from the shell.
Switch to the images database, and query for the first entry in the fs.files collec-
tion. You'll see the metadata for the file you just stored:
> use images
> db.fs.files.findOne()
{
"_id" : ObjectId("4d606588238d3b4471000001"),
"filename" : "canyon.jpg",
"contentType" : "binary/octet-stream",
"length" : 2004828,
"chunkSize" : 262144,
"uploadDate" : ISODate("2011-02-20T00:51:21.191Z"),
"md5" : "9725ad463b646ccbd287be87cb9b1f6e"
}
Search WWH ::




Custom Search