mongodb/tutorial.md

135 lines
3 KiB
Markdown
Raw Normal View History

2010-02-28 12:19:02 +00:00
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
2010-03-09 05:13:01 +00:00
of the Haskell mongoDB drivers. It is modeled after the
[pymongo tutorial](http://api.mongodb.org/python/1.4%2B/tutorial.html).
2010-02-28 12:19:02 +00:00
You will need the mongoDB bindings installed as well as mongo itself installed.
2010-03-09 05:13:01 +00:00
$ = command line prompt
> = ghci repl prompt
2010-02-28 12:19:02 +00:00
Installing Haskell Bindings
---------------------------
From Source:
2010-03-01 14:15:40 +00:00
2010-03-09 05:13:01 +00:00
$ git clone git://github.com/srp/mongoDB.git
$ cd mongoDB
$ runhaskell Setup.hs configure
$ runhaskell Setup.hs build
$ runhaskell Setup.hs install
2010-02-28 12:19:02 +00:00
From Hackage using cabal:
2010-03-09 05:13:01 +00:00
$ cabal install mongoDB
2010-02-28 12:19:02 +00:00
Getting Ready
-------------
Start a MongoDB instance for us to play with:
2010-03-09 05:13:01 +00:00
$ mongod
2010-02-28 12:19:02 +00:00
Start up a haskell repl:
2010-03-09 05:13:01 +00:00
$ ghci
2010-02-28 12:19:02 +00:00
Now We'll need to bring in the MongoDB/BSON bindings:
2010-03-09 05:13:01 +00:00
> import Database.MongoDB
> import Database.MongoDB.BSON
2010-02-28 12:19:02 +00:00
2010-03-01 14:15:40 +00:00
Making A Connection
2010-02-28 12:19:02 +00:00
-------------------
Open up a connection to your DB instance, using the standard port:
2010-03-09 05:13:01 +00:00
> con <- connect "127.0.0.1" []
2010-02-28 12:19:02 +00:00
or for a non-standard port
2010-03-09 05:13:01 +00:00
> import Network
> con <- connectOnPort "127.0.0.1" (Network.PortNumber 666) []
By default mongoDB will try to find the master and connect to it and
will throw an exception if a master can not be found to connect
to. You can force mongoDB to connect to the slave by adding SlaveOK as
a connection option, eg:
2010-03-09 05:13:01 +00:00
> con <- connect "127.0.0.1" [SlaveOK]
2010-02-28 12:19:02 +00:00
Getting the Databases
------------------
2010-03-09 05:13:01 +00:00
> dbs <- databaseNames con
> let testdb = head dbs
2010-03-01 14:15:40 +00:00
2010-02-28 12:19:02 +00:00
Getting the Collections
-----------------------
2010-03-09 05:13:01 +00:00
> collections <- collectionNames con testdb
> let testcol = head collections
2010-02-28 12:19:02 +00:00
Documents
---------
BSON representation in Haskell
Inserting a Document
-------------------
2010-03-09 05:13:01 +00:00
> insert con testcol (toBsonDoc [("author", toBson "Mike"), ("text", toBson "My first Blog post!"), ("tags", toBson ["mongodb", "python","pymongo"])])
2010-02-28 12:19:02 +00:00
Getting a single document with findOne
-------------------------------------
2010-03-09 05:13:01 +00:00
> findOne con curcol (toBsonDoc [("author", toBson "Mike")])
2010-02-28 12:19:02 +00:00
Querying for More Than One Document
------------------------------------
2010-03-09 05:13:01 +00:00
> cursor <- find con curcol (toBsonDoc [("author", toBson "Mike")])
> allDocs cursor
2010-02-28 12:19:02 +00:00
You can combine these into one line:
2010-03-09 05:13:01 +00:00
> docs <- allDocs =<< find con curcol (toBsonDoc [("author", toBson "Mike")])
See nextDoc to modify cursor incrementally one at a time.
2010-02-28 12:19:02 +00:00
* Note: allDocs automatically closes the cursor when done, through nextDoc.
Counting
--------
2010-03-09 05:13:01 +00:00
We can count how many documents are in an entire collection:
2010-02-28 12:19:02 +00:00
2010-03-09 05:13:01 +00:00
> num <- count con testcol
2010-02-28 12:19:02 +00:00
2010-03-09 05:13:01 +00:00
Or we can query for how many documents match a query:
2010-02-28 12:19:02 +00:00
2010-03-09 05:13:01 +00:00
> num <- countMatching con testcol (toBsonDoc [("author", toBson "Mike")])
2010-02-28 12:19:02 +00:00
Range Queries
-------------
2010-03-09 05:13:01 +00:00
No non native sorting yet.
2010-02-28 12:19:02 +00:00
Indexing
--------
2010-03-09 05:13:01 +00:00
WIP - coming soon.
Something like...
2010-02-28 12:19:02 +00:00
2010-03-09 05:13:01 +00:00
> index <- createIndex con testcol [("author", Ascending)] True