Skip to content

Instantly share code, notes, and snippets.

@seanhess
Created November 3, 2011 21:40
Show Gist options
  • Save seanhess/1337864 to your computer and use it in GitHub Desktop.
Save seanhess/1337864 to your computer and use it in GitHub Desktop.
Mongo Example
{-# 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