2014-07-07 05:05:03 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE ExtendedDefaultRules #-}
|
|
|
|
|
|
|
|
module Main (main) where
|
2011-07-09 02:17:44 +00:00
|
|
|
|
2014-03-02 05:55:15 +00:00
|
|
|
import Database.MongoDB (Action, Document, Document, Value, access,
|
|
|
|
close, connect, delete, exclude, find,
|
|
|
|
host, insertMany, master, project, rest,
|
2014-07-07 05:05:03 +00:00
|
|
|
select, sort, (=:))
|
2011-07-09 02:17:44 +00:00
|
|
|
import Control.Monad.Trans (liftIO)
|
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
main :: IO ()
|
2011-07-09 02:17:44 +00:00
|
|
|
main = do
|
2014-07-07 05:05:03 +00:00
|
|
|
pipe <- connect (host "127.0.0.1")
|
2011-07-09 02:17:44 +00:00
|
|
|
e <- access pipe master "baseball" run
|
|
|
|
close pipe
|
|
|
|
print e
|
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
run :: Action IO ()
|
2011-07-09 02:17:44 +00:00
|
|
|
run = do
|
|
|
|
clearTeams
|
|
|
|
insertTeams
|
2011-07-21 20:39:19 +00:00
|
|
|
allTeams >>= printDocs "All Teams"
|
|
|
|
nationalLeagueTeams >>= printDocs "National League Teams"
|
|
|
|
newYorkTeams >>= printDocs "New York Teams"
|
2011-07-09 02:17:44 +00:00
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
clearTeams :: Action IO ()
|
2011-07-09 02:17:44 +00:00
|
|
|
clearTeams = delete (select [] "team")
|
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
insertTeams :: Action IO [Value]
|
2011-07-09 02:17:44 +00:00
|
|
|
insertTeams = insertMany "team" [
|
2011-07-30 16:49:37 +00:00
|
|
|
["name" =: "Yankees", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "American"],
|
|
|
|
["name" =: "Mets", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "National"],
|
|
|
|
["name" =: "Phillies", "home" =: ["city" =: "Philadelphia", "state" =: "PA"], "league" =: "National"],
|
|
|
|
["name" =: "Red Sox", "home" =: ["city" =: "Boston", "state" =: "MA"], "league" =: "American"] ]
|
2011-07-09 02:17:44 +00:00
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
allTeams :: Action IO [Document]
|
2011-07-30 16:49:37 +00:00
|
|
|
allTeams = rest =<< find (select [] "team") {sort = ["home.city" =: 1]}
|
2011-07-09 02:17:44 +00:00
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
nationalLeagueTeams :: Action IO [Document]
|
2011-07-30 16:49:37 +00:00
|
|
|
nationalLeagueTeams = rest =<< find (select ["league" =: "National"] "team")
|
2011-07-09 02:17:44 +00:00
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
newYorkTeams :: Action IO [Document]
|
2011-07-30 16:49:37 +00:00
|
|
|
newYorkTeams = rest =<< find (select ["home.state" =: "NY"] "team") {project = ["name" =: 1, "league" =: 1]}
|
2011-07-09 02:17:44 +00:00
|
|
|
|
2014-03-02 05:54:56 +00:00
|
|
|
printDocs :: String -> [Document] -> Action IO ()
|
2011-07-09 02:17:44 +00:00
|
|
|
printDocs title docs = liftIO $ putStrLn title >> mapM_ (print . exclude ["_id"]) docs
|