BSON

Moped::BSON is the namespace for Moped's BSON implementation. It's implemented in pure (but fast) ruby. The public entry point into the Moped::BSON module is Moped::BSON::Document, which is just a subclass of Hash, but exposes two class methods: serialize and deserialize. serialize accepts a Moped::BSON::Document (or Hash) and returns the serialized BSON representation. deserialize does the opposite: it reads data from an IO-like input and returns a deserialized Moped::BSON::Document.

Types

Object Ids

The Moped::BSON::ObjectId class is used for generating and interacting with Mongo's ids.

id = Moped::BSON::ObjectId.new # => 4f8583b5e5a4e46a64000002
id.generation_time # => 2012-04-11 13:14:29 UTC
id == Moped::BSON::ObjectId.from_string(id.to_s) # => true

The following is a table of the object id API.

Operation Syntax
ObjectId.new

Instantiate a new object id.

Moped::BSON::ObjectId.new
ObjectId.from_string

Instantiate a new object id from a string.

Moped::BSON::ObjectId.from_string("4f8d8c66e5a4e45396000009")
ObjectId.from_time

Instantiate a new object id from a time.

Moped::BSON::ObjectId.from_time(Time.new)
ObjectId.legal?

Checks if a string is a valid object id.

Moped::BSON::ObjectId.legal?("4f8d8c66e5a4e45396000009")

Code

The Moped::BSON::Code class is used for working with javascript on the server.

Moped::BSON::Code.new("function () { return this.name }")
Moped::BSON::Code.new("function (s) { return s.prefix + this.name }", prefix: "_")

Binary Data

The Moped::BSON::Binary class allows you to persist binary data to the server, and supports the following types: :generic, :function, :old, :uuid, :md5, and :user. Note that :old is deprecated, but still present to support legacy data.

Moped::BSON::Binary.new(:md5, Digest::MD5.digest(__FILE__))