From e65bbddc3d1537a0ed04333808b613aab45ada9f Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sun, 28 Feb 2010 07:19:02 -0500 Subject: [PATCH] Add mini-tutorial --- TODO | 1 - tutorial.md | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 tutorial.md diff --git a/TODO b/TODO index 3a57a81..706aefe 100644 --- a/TODO +++ b/TODO @@ -89,7 +89,6 @@ Misc Tests? Documentation - ref - - tutorial GridFS diff --git a/tutorial.md b/tutorial.md new file mode 100644 index 0000000..09e9308 --- /dev/null +++ b/tutorial.md @@ -0,0 +1,129 @@ +MongoDB Haskell Mini Tutorial +----------------------------- + + __Author:__ Brian Gianforcaro (b.gianfo@gmail.com) + + __Updated:__ 2/28/2010 + +This is a mini tutorial to get you up and going with the basics +of the Haskell mongoDB drivers. It is modeled after the python tutorial +pymongo available here: http://api.mongodb.org/python/1.4%2B/tutorial.html + +You will need the mongoDB bindings installed as well as mongo itself installed. + +>$ = command line prompt +> = ghci repl prompt + + +Installing Haskell Bindings +--------------------------- + +From Source: + +> $ git clone git://github.com/srp/mongoDB.git + +> $ cd mongoDB + +> $ runhaskell Setup.hs configure + +> $ runhaskell Setup.hs build + +> $ runhaskell Setup.hs install + +From Hackage using cabal: + +> $ cabal install mongoDB + + +Getting Ready +------------- + +Start a MongoDB instance for us to play with: + +> $ mongod + +Start up a haskell repl: + +> $ ghci + +Now We'll need to bring in the MongoDB/BSON bindings: + +> import Database.MongoDB + +> import Database.MongoDB.BSON + +Making A Connection +------------------- +Open up a connection to your DB instance, using the standard port: + +> con <- connect "127.0.0.1" + +or for a non-standard port + +> import Network +> con <- connectOnPort "127.0.0.1" (Network.PortNumber 666) + +Getting the Databases +------------------ + +> dbs <- databaseNames con +> let testdb = head dbs + + +Getting the Collections +----------------------- + +> collections <- collectionNames con testdb +> let testcol = head collections + +Documents +--------- + +BSON representation in Haskell + +Inserting a Document +------------------- + +> insert con testcol (toBsonDoc [("author", toBson "Mike"), ("text", toBson "My first Blog post!"), ("tags", toBson ["mongodb", "python","pymongo"])]) + + +Getting a single document with findOne +------------------------------------- + +> findOne con curcol (toBsonDoc [("author", toBson "Mike")]) + +Querying for More Than One Document +------------------------------------ + +> cursor <- find con curcol (toBsonDoc [("author", toBson "Mike")]) + +> allDocs cursor + + See nextDoc to modify cursor incrementally one at a time. + + * Note: allDocs automatically closes the cursor when done, through nextDoc. + + +Counting +-------- + + We can count how many documents are in an entire collection: + +> num <- count con testcol + + Or we can query for how many documents match a query: + +> num <- countMatching con testcol (toBsonDoc [("author", toBson "Mike")]) + +Range Queries +------------- + + No non native sorting yet. + +Indexing +-------- + + WIP - coming soon. + + Something like... +> index <- createIndex con testcol [("author", Ascending)] True