bson-0.0.3: BSON documents are JSON-like objects with a standard binary encoding




A BSON document is a JSON-like object with a standard binary encoding defined at This implements version 1.0 of that spec.

Use the GHC language extension OverloadedStrings to automatically convert String literals to UString (UTF8)



type UString = CompactStringSource

UTF-8 String


type Document = [Field]Source

A BSON document is a list of Fields

look :: Monad m => Label -> Document -> m ValueSource

Value of field in document, or fail (Nothing) if field not found

lookup :: (Val v, Monad m) => Label -> Document -> m vSource

Lookup value of field in document and cast to expected type. Fail (Nothing) if field not found or value not of expected type.

valueAt :: Label -> Document -> ValueSource

Value of field in document. Error if missing.

at :: forall v. Val v => Label -> Document -> vSource

Typed value of field in document. Error if missing or wrong type.

include :: [Label] -> Document -> DocumentSource

Only include fields of document in label list

exclude :: [Label] -> Document -> DocumentSource

Exclude fields from document in label list

merge :: Document -> Document -> DocumentSource

Merge documents with preference given to first one when both have the same label. I.e. for every (k := v) in first argument, if k exists in second argument then replace its value with v, otherwise add (k := v) to second argument.


data Field Source

A BSON field is a named value, where the name (label) is a string and the value is a BSON Value




label :: Label
value :: Value

(=:) :: Val v => Label -> v -> FieldSource

Field with given label and typed value

(=?) :: Val a => Label -> Maybe a -> DocumentSource

If Just value then return one field document, otherwise return empty document

type Label = UStringSource

The name of a BSON field


class (Typeable a, Show a, Eq a) => Val a whereSource

Haskell types of this class correspond to BSON value types


val :: a -> ValueSource

cast' :: Value -> Maybe aSource

fval :: (forall a. Val a => a -> b) -> Value -> bSource

Apply generic function to typed value

cast :: forall m a. (Val a, Monad m) => Value -> m aSource

Convert Value to expected type, or fail (Nothing) if not of that type

typed :: Val a => Value -> aSource

Convert Value to expected type. Error if not that type.

typeOfVal :: Value -> TypeRepSource

Type of typed value

Special Bson value types

newtype UUID Source


UUID ByteString 

newtype MD5 Source


MD5 ByteString 


data Javascript Source

Javascript code with possibly empty environment mapping variables to values that the code may reference


data ObjectId Source

A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order.


Oid Word32 Word64 

timestamp :: ObjectId -> UTCTimeSource

Time when objectId was created

genObjectId :: IO ObjectIdSource

Create a fresh ObjectId