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

Safe HaskellNone

Data.Bson

Contents

Description

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

Use the GHC language extension OverloadedStrings to automatically convert String literals to Text

Synopsis

Document

type Document = [Field]

A BSON document is a list of Fields

(!?) :: Val a => Document -> Label -> Maybe a

Recursively lookup a nested field in a Document.

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

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

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

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 -> Value

Value of field in document. Error if missing.

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

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

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

Only include fields of document in label list

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

Exclude fields from document in label list

merge :: Document -> Document -> Document

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.

Field

data Field

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

Constructors

:= 

Fields

label :: !Label
 
value :: Value
 

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

Field with given label and typed value

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

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

type Label = Text

The name of a BSON field

Value

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

Haskell types of this class correspond to BSON value types

Methods

val :: a -> Value

cast' :: Value -> Maybe a

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

Apply generic function to typed value

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

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

typed :: Val a => Value -> a

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

typeOfVal :: Value -> TypeRep

Type of typed value

Special Bson value types

newtype UUID

Constructors

UUID ByteString 

newtype MD5

Constructors

MD5 ByteString 

data Regex

The first string is the regex pattern, the second is the regex options string. Options are identified by characters, which must be listed in alphabetical order. Valid options are *i* for case insensitive matching, *m* for multiline matching, *x* for verbose mode, *l* to make \w, \W, etc. locale dependent, *s* for dotall mode ("." matches everything), and *u* to make \w, \W, etc. match unicode.

Constructors

Regex Text Text 

data Javascript

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

Constructors

Javascript Document Text 

newtype Symbol

Constructors

Symbol Text 

ObjectId

data ObjectId

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.

Constructors

Oid Word32 Word64 

timestamp :: ObjectId -> UTCTime

Time when objectId was created

genObjectId :: IO ObjectId

Create a fresh ObjectId