Database Reference
In-Depth Information
into a local variable. Next, you wrap the raw binary data as a BSON binary object using
the Ruby driver's BSON::Binary constructor:
require 'rubygems'
require 'mongo'
image_filename = File.join(File.dirname(__FILE__), "canyon-thumb.jpg")
image_data = File.open(image_filename).read
bson_image_data = BSON::Binary.new(image_data)
All that remains is to build a simple document to contain the binary data and then
insert it into the database:
doc = {"name" => "monument-thumb.jpg",
"data" => bson_image_data }
@con = Mongo::Connection.new
@thumbnails = @con['images']['thumbnails']
@image_id = @thumbnails.insert(doc)
To e x t r a c t t h e b in a r y d a t a , f e t c h t h e d o c u m e n t . I n R u b y, t h e to_s method unpacks the
data into a binary string, and you can use this to compare the saved data to the original:
doc = @thumbnails.find_one({"_id" => @image_id})
if image_data == doc["data"].to_s
puts "Stored image is equal to the original file!"
end
If you run the preceding script, you'll see a message indicating that the two files are
indeed the same.
C.1.2
Storing an MD5
It's common to store a checksum as binary data, and this marks another potential use
of the BSON binary type. Here's how you can generate an MD5 of the thumbnail and
add it to the document just stored:
require 'md5'
md5 = Digest::MD5.file(image_filename).digest
bson_md5 = BSON::Binary.new(md5, BSON::Binary::SUBTYPE_MD5)
@thumbnails.update({:_id => @image_id}, {"$set" => {:md5 => bson_md5}})
Note that when creating the BSON binary object, you tag the data with SUBTYPE_MD5 .
The subtype is an extra field on the BSON binary type that indicates what kind of
binary data is being stored. However, this field is entirely optional and has no effect on
how the database stores or interprets the data. 1
It's easy to query for the document just stored, but do notice that you exclude the
data field to keep the return document small and readable:
1
This wasn't always technically true. The deprecated default subtype of 2 indicated that the attached binary
data also included four extra bytes to indicate the size, and this did affect a few database commands. The cur-
rent default subtype is 0, and all subtypes now store the binary payload the same way. Subtype can therefore
be seen as a kind of lightweight tag to be optionally used by application developers.
Search WWH ::




Custom Search