Merge pull request #21 from fujimura/update-example-hs

Update example and readme
This commit is contained in:
Sergei Lebedev 2014-03-02 13:15:17 +03:00
commit e8f6341756
2 changed files with 16 additions and 5 deletions

View file

@ -1,9 +1,9 @@
This is the Haskell MongoDB driver (client). [MongoDB](http://www.mongodb.org) is a free, scalable, fast, document database management system. This driver lets you connect to a MongoDB server, and update and query its data. It also lets you do adminstrative tasks, like create an index or look at performance statistics. This is the Haskell MongoDB driver (client). [MongoDB](http://www.mongodb.org) is a free, scalable, fast, document database management system. This driver lets you connect to a MongoDB server, and update and query its data. It also lets you do adminstrative tasks, like create an index or look at performance statistics.
### Documentation ### Documentation
* [Quick example](http://github.com/TonyGen/mongoDB-haskell/blob/master/doc/Example.hs) * [Quick example](http://github.com/selectel/mongoDB-haskell/blob/master/doc/Example.hs)
* [Tutorial](http://github.com/TonyGen/mongoDB-haskell/blob/master/doc/tutorial.md) * [Tutorial](http://github.com/selectel/mongoDB-haskell/blob/master/doc/tutorial.md)
* [Driver API](http://hackage.haskell.org/package/mongoDB) * [Driver API](http://hackage.haskell.org/package/mongoDB)
* [MapReduce example](http://github.com/TonyGen/mongoDB-haskell/blob/master/doc/map-reduce-example.md) * [MapReduce example](http://github.com/selectel/mongoDB-haskell/blob/master/doc/map-reduce-example.md)
* [Driver design](http://github.com/TonyGen/mongoDB-haskell/blob/master/doc/Article1.md) * [Driver design](http://github.com/selectel/mongoDB-haskell/blob/master/doc/Article1.md)
* [MongoDB DBMS](http://www.mongodb.org) * [MongoDB DBMS](http://www.mongodb.org)

View file

@ -1,14 +1,19 @@
{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-} {-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-}
import Database.MongoDB import Database.MongoDB (Action, Document, Document, Value, access,
close, connect, delete, exclude, find,
host, insertMany, master, project, rest,
runIOE, select, sort, (=:))
import Control.Monad.Trans (liftIO) import Control.Monad.Trans (liftIO)
main :: IO ()
main = do main = do
pipe <- runIOE $ connect (host "127.0.0.1") pipe <- runIOE $ connect (host "127.0.0.1")
e <- access pipe master "baseball" run e <- access pipe master "baseball" run
close pipe close pipe
print e print e
run :: Action IO ()
run = do run = do
clearTeams clearTeams
insertTeams insertTeams
@ -16,18 +21,24 @@ run = do
nationalLeagueTeams >>= printDocs "National League Teams" nationalLeagueTeams >>= printDocs "National League Teams"
newYorkTeams >>= printDocs "New York Teams" newYorkTeams >>= printDocs "New York Teams"
clearTeams :: Action IO ()
clearTeams = delete (select [] "team") clearTeams = delete (select [] "team")
insertTeams :: Action IO [Value]
insertTeams = insertMany "team" [ insertTeams = insertMany "team" [
["name" =: "Yankees", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "American"], ["name" =: "Yankees", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "American"],
["name" =: "Mets", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "National"], ["name" =: "Mets", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "National"],
["name" =: "Phillies", "home" =: ["city" =: "Philadelphia", "state" =: "PA"], "league" =: "National"], ["name" =: "Phillies", "home" =: ["city" =: "Philadelphia", "state" =: "PA"], "league" =: "National"],
["name" =: "Red Sox", "home" =: ["city" =: "Boston", "state" =: "MA"], "league" =: "American"] ] ["name" =: "Red Sox", "home" =: ["city" =: "Boston", "state" =: "MA"], "league" =: "American"] ]
allTeams :: Action IO [Document]
allTeams = rest =<< find (select [] "team") {sort = ["home.city" =: 1]} allTeams = rest =<< find (select [] "team") {sort = ["home.city" =: 1]}
nationalLeagueTeams :: Action IO [Document]
nationalLeagueTeams = rest =<< find (select ["league" =: "National"] "team") nationalLeagueTeams = rest =<< find (select ["league" =: "National"] "team")
newYorkTeams :: Action IO [Document]
newYorkTeams = rest =<< find (select ["home.state" =: "NY"] "team") {project = ["name" =: 1, "league" =: 1]} newYorkTeams = rest =<< find (select ["home.state" =: "NY"] "team") {project = ["name" =: 1, "league" =: 1]}
printDocs :: String -> [Document] -> Action IO ()
printDocs title docs = liftIO $ putStrLn title >> mapM_ (print . exclude ["_id"]) docs printDocs title docs = liftIO $ putStrLn title >> mapM_ (print . exclude ["_id"]) docs