Database Reference
In-Depth Information
One of the incidental benefits of using MongoDB object
ID
s is that they include a
timestamp. Most of the drivers allow you to extract the timestamp, thus providing the
document creation time, with resolution to the nearest second, for free. Using the
Ruby driver, you can call an object
ID
's
generation_time
method to get that
ID
's cre-
ation time as a Ruby
Time
object:
irb(main):002:0> id = BSON::ObjectId.new
=> BSON::ObjectId('4c41e78f238d3b9090000001')
irb(main):003:0> id.generation_time
=> Sat Jul 17 17:25:35 UTC 2010
Naturally, you can also use object
ID
s to issue range queries on object creation time.
For instance, if you wanted to query for all documents created between October 2010
and November 2010, you could create two object
ID
s whose timestamps encode those
dates and then issue a range query on
_id
. Since Ruby provides methods for generat-
ing object
ID
s from any
Time
object, the code for doing this is trivial:
oct_id = BSON::ObjectId.from_time(Time.utc(2010, 10, 1))
nov_id = BSON::ObjectId.from_time(Time.utc(2010, 11, 1))
@users.find({'_id' => {'$gte' => oct_id, '$lt' => nov_id}})
I've explained the rationale for MongoDB object
ID
s and the meaning behind the
bytes. All that remains is to see how they're encoded. That's the subject of the next
section, where we discuss
BSON
.
3.2.2
BSON
BSON
is the binary format used to represent documents in MongoDB.
BSON
acts as
both a storage and command format: all documents are stored on disk as
BSON
, and
all queries and commands are specified using
BSON
documents. Consequently, all
MongoDB drivers must be able to translate between some language-specific document
representation and
BSON
.
BSON
defines the data types you can use with MongoDB. Knowing which types
BSON
comprises, as well as a bit about their encoding, will take you a long way in using
MongoDB effectively and diagnosing certain performance issues when they occur.
At the time of this writing, the
BSON
specification includes 19 data types. What this
means is that each value within a document must be convertible into one of these
types in order to be stored in MongoDB. The
BSON
types include many that you'd
expect:
UTF
-8 string, 32- and 64-bit integer, double, Boolean, timestamp, and
UTC
datetime. But a number of types are specific to MongoDB. For instance, the object
ID
format described in the previous section gets its own type; there's a binary type for
opaque blobs; and there's even a symbol type for languages that support it.
Figure 3.2 illustrates how you serialize a Ruby hash into a bona fide
BSON
docu-
ment. The Ruby document contains an object
ID
and a string. When translated to a
BSON
document, what comes first is a 4-byte header indicating the document's size
(you can see that this one is 38 bytes). Next are the two key-value pairs. Each pair
begins with a byte denoting its type, followed by a null-terminated string for the key