Created
November 3, 2011 21:40
-
-
Save seanhess/1337864 to your computer and use it in GitHub Desktop.
Mongo Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-} | |
{- | |
db <- mdb | |
db $ delete $ select [] "tags" | |
-} | |
module Mongo (mdb) where | |
import Database.MongoDB | |
import Data.Bson | |
import Data.CompactString | |
import Control.Monad.IO.Class | |
main = do | |
-- Here's the normal way you access mongo stuff | |
-- I want to put it all in one function, and return a function called "db" | |
-- so I can think of it more like normal mongo: db.mycollection.remove({}) | |
pipe <- runIOE $ connect $ host "127.0.0.1" | |
let run action = access pipe master "testdb" action | |
run $ delete $ select [] "mycollection" | |
-- here's what the shortcut looks like | |
db <- mdb "127.0.0.1" "testdb" | |
db $ delete $ select [] "mycollection" | |
-- but without the above line, I get the error pasted at the bottom. Why? | |
return () | |
mdb :: (MonadIO m) => String -> String -> IO (Action m a -> m (Either Failure a)) | |
mdb hostname dbname = do | |
pipe <- runIOE $ connect $ host hostname | |
return (access pipe master (pack dbname)) | |
{- | |
/Users/seanhess/itv/tvtagdemos/Mongo.hs:24:11: | |
Ambiguous type variable `m0' in the constraint: | |
(MonadIO m0) arising from a use of `mdb' | |
Probable fix: add a type signature that fixes these type variable(s) | |
In a stmt of a 'do' expression: db <- mdb "127.0.0.1" "testdb" | |
In the expression: | |
do { pipe <- runIOE $ connect $ host "127.0.0.1"; | |
let run action = access pipe master "testdb" action; | |
run $ delete $ select [] "mycollection"; | |
db <- mdb "127.0.0.1" "testdb"; | |
.... } | |
In an equation for `main': | |
main | |
= do { pipe <- runIOE $ connect $ host "127.0.0.1"; | |
let run action = ...; | |
run $ delete $ select [] "mycollection"; | |
.... } | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment