changed formatting in tutorial
This commit is contained in:
parent
f23d44195b
commit
d1366e2261
1 changed files with 36 additions and 42 deletions
78
tutorial.md
78
tutorial.md
|
@ -6,13 +6,13 @@ MongoDB Haskell Mini Tutorial
|
||||||
__Updated:__ 2/28/2010
|
__Updated:__ 2/28/2010
|
||||||
|
|
||||||
This is a mini tutorial to get you up and going with the basics
|
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
|
of the Haskell mongoDB drivers. It is modeled after the
|
||||||
pymongo available here: http://api.mongodb.org/python/1.4%2B/tutorial.html
|
[pymongo tutorial](http://api.mongodb.org/python/1.4%2B/tutorial.html).
|
||||||
|
|
||||||
You will need the mongoDB bindings installed as well as mongo itself installed.
|
You will need the mongoDB bindings installed as well as mongo itself installed.
|
||||||
|
|
||||||
>$ = command line prompt
|
$ = command line prompt
|
||||||
> = ghci repl prompt
|
> = ghci repl prompt
|
||||||
|
|
||||||
|
|
||||||
Installing Haskell Bindings
|
Installing Haskell Bindings
|
||||||
|
@ -20,68 +20,62 @@ Installing Haskell Bindings
|
||||||
|
|
||||||
From Source:
|
From Source:
|
||||||
|
|
||||||
> $ git clone git://github.com/srp/mongoDB.git
|
$ git clone git://github.com/srp/mongoDB.git
|
||||||
|
$ cd mongoDB
|
||||||
> $ cd mongoDB
|
$ runhaskell Setup.hs configure
|
||||||
|
$ runhaskell Setup.hs build
|
||||||
> $ runhaskell Setup.hs configure
|
$ runhaskell Setup.hs install
|
||||||
|
|
||||||
> $ runhaskell Setup.hs build
|
|
||||||
|
|
||||||
> $ runhaskell Setup.hs install
|
|
||||||
|
|
||||||
From Hackage using cabal:
|
From Hackage using cabal:
|
||||||
|
|
||||||
> $ cabal install mongoDB
|
$ cabal install mongoDB
|
||||||
|
|
||||||
|
|
||||||
Getting Ready
|
Getting Ready
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
Start a MongoDB instance for us to play with:
|
Start a MongoDB instance for us to play with:
|
||||||
|
|
||||||
> $ mongod
|
$ mongod
|
||||||
|
|
||||||
Start up a haskell repl:
|
Start up a haskell repl:
|
||||||
|
|
||||||
> $ ghci
|
$ ghci
|
||||||
|
|
||||||
Now We'll need to bring in the MongoDB/BSON bindings:
|
Now We'll need to bring in the MongoDB/BSON bindings:
|
||||||
|
|
||||||
> import Database.MongoDB
|
> import Database.MongoDB
|
||||||
|
> import Database.MongoDB.BSON
|
||||||
> import Database.MongoDB.BSON
|
|
||||||
|
|
||||||
Making A Connection
|
Making A Connection
|
||||||
-------------------
|
-------------------
|
||||||
Open up a connection to your DB instance, using the standard port:
|
Open up a connection to your DB instance, using the standard port:
|
||||||
|
|
||||||
> con <- connect "127.0.0.1" []
|
> con <- connect "127.0.0.1" []
|
||||||
|
|
||||||
or for a non-standard port
|
or for a non-standard port
|
||||||
|
|
||||||
> import Network
|
> import Network
|
||||||
> con <- connectOnPort "127.0.0.1" (Network.PortNumber 666) []
|
> con <- connectOnPort "127.0.0.1" (Network.PortNumber 666) []
|
||||||
|
|
||||||
By default mongoDB will try to find the master and connect to it and
|
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
|
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
|
to. You can force mongoDB to connect to the slave by adding SlaveOK as
|
||||||
a connection option, eg:
|
a connection option, eg:
|
||||||
|
|
||||||
> con <- connect "127.0.0.1" [SlaveOK]
|
> con <- connect "127.0.0.1" [SlaveOK]
|
||||||
|
|
||||||
Getting the Databases
|
Getting the Databases
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
> dbs <- databaseNames con
|
> dbs <- databaseNames con
|
||||||
> let testdb = head dbs
|
> let testdb = head dbs
|
||||||
|
|
||||||
|
|
||||||
Getting the Collections
|
Getting the Collections
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
> collections <- collectionNames con testdb
|
> collections <- collectionNames con testdb
|
||||||
> let testcol = head collections
|
> let testcol = head collections
|
||||||
|
|
||||||
Documents
|
Documents
|
||||||
---------
|
---------
|
||||||
|
@ -91,24 +85,23 @@ BSON representation in Haskell
|
||||||
Inserting a Document
|
Inserting a Document
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
> insert con testcol (toBsonDoc [("author", toBson "Mike"), ("text", toBson "My first Blog post!"), ("tags", toBson ["mongodb", "python","pymongo"])])
|
> insert con testcol (toBsonDoc [("author", toBson "Mike"), ("text", toBson "My first Blog post!"), ("tags", toBson ["mongodb", "python","pymongo"])])
|
||||||
|
|
||||||
|
|
||||||
Getting a single document with findOne
|
Getting a single document with findOne
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
> findOne con curcol (toBsonDoc [("author", toBson "Mike")])
|
> findOne con curcol (toBsonDoc [("author", toBson "Mike")])
|
||||||
|
|
||||||
Querying for More Than One Document
|
Querying for More Than One Document
|
||||||
------------------------------------
|
------------------------------------
|
||||||
|
|
||||||
> cursor <- find con curcol (toBsonDoc [("author", toBson "Mike")])
|
> cursor <- find con curcol (toBsonDoc [("author", toBson "Mike")])
|
||||||
|
> allDocs cursor
|
||||||
> allDocs cursor
|
|
||||||
|
|
||||||
You can combine these into one line:
|
You can combine these into one line:
|
||||||
|
|
||||||
> docs <- allDocs =<< find con curcol (toBsonDoc [("author", toBson "Mike")])
|
> docs <- allDocs =<< find con curcol (toBsonDoc [("author", toBson "Mike")])
|
||||||
|
|
||||||
See nextDoc to modify cursor incrementally one at a time.
|
See nextDoc to modify cursor incrementally one at a time.
|
||||||
|
|
||||||
|
@ -118,23 +111,24 @@ See nextDoc to modify cursor incrementally one at a time.
|
||||||
Counting
|
Counting
|
||||||
--------
|
--------
|
||||||
|
|
||||||
We can count how many documents are in an entire collection:
|
We can count how many documents are in an entire collection:
|
||||||
|
|
||||||
> num <- count con testcol
|
> num <- count con testcol
|
||||||
|
|
||||||
Or we can query for how many documents match a query:
|
Or we can query for how many documents match a query:
|
||||||
|
|
||||||
> num <- countMatching con testcol (toBsonDoc [("author", toBson "Mike")])
|
> num <- countMatching con testcol (toBsonDoc [("author", toBson "Mike")])
|
||||||
|
|
||||||
Range Queries
|
Range Queries
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
No non native sorting yet.
|
No non native sorting yet.
|
||||||
|
|
||||||
Indexing
|
Indexing
|
||||||
--------
|
--------
|
||||||
|
|
||||||
WIP - coming soon.
|
WIP - coming soon.
|
||||||
|
|
||||||
Something like...
|
Something like...
|
||||||
> index <- createIndex con testcol [("author", Ascending)] True
|
|
||||||
|
> index <- createIndex con testcol [("author", Ascending)] True
|
||||||
|
|
Loading…
Reference in a new issue