This module aims to make mapping between algebraic data types and bson documents easy.
You can also generate documents with selectFields
, which takes a
list of functions names that of type a -> b and returns a function
of type a -> Document.
Example:
import Data.Bson.Mapping import Data.Time.Clock import Data.Data (Data, Typeable) data Post = Post { time :: UTCTime , author :: String , content :: String , votes :: Int } deriving (Show, Read, Eq, Ord, Data, Typeable) $(deriveBson ''Post) main :: IO () main = do now <- getCurrentTime let post = Post now "francesco" "lorem ipsum" 5 (fromBson (toBson post) :: IO Post) >>= print print $ toBson post print $ $(selectFields ['time, 'content]) post
Documentation
selectFields :: [Name] -> Q ExpSource
Select only certain fields in a document, see the code sample at the top.
Please note that there is no checking for the names to be actual fields of the bson document mapped to a datatype, so be careful.
getConsDoc :: Name -> Q ExpSource
Get a document that identifies the data type - getConsDoc ''Post
.
This is useful to select all documents mapped to a certain data type.
subDocument :: Label -> Document -> DocumentSource
Simple function to select fields in a nested document.