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) []
|
2010-03-01 14:27:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
Databases, Collections and FullCollections
|
|
|
|
------------------------------------------
|
2010-02-28 12:19:02 +00:00
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
As many database servers, MongoDB has databases--separate namespaces
|
|
|
|
under which collections reside. Most of the APIs for this driver
|
|
|
|
request the *FullCollection* which is simply the *Database* and the
|
|
|
|
*Collection* concatenated with a period.
|
|
|
|
|
|
|
|
For instance 'myweb_prod.users' is the the *FullCollection* name for
|
|
|
|
the *Collection 'users' in the database 'myweb_prod'.
|
|
|
|
|
|
|
|
Databases and collections do not need to be created, just start using
|
|
|
|
them and MongoDB will automatically create them for you.
|
|
|
|
|
|
|
|
In the below examples we'll be using the following *FullCollection*:
|
2010-03-01 14:15:40 +00:00
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
> import Data.ByteString.Lazy.UTF8
|
|
|
|
> let testcol = (fromString "test.haskell")
|
|
|
|
|
|
|
|
You can obtain a list of databases available on a connection:
|
|
|
|
|
|
|
|
> dbs <- databaseNames con
|
2010-02-28 12:19:02 +00:00
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
You can obtain a list of collections available on a database:
|
2010-02-28 12:19:02 +00:00
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
> collections <- collectionNames con (fromString "test")
|
2010-02-28 12:19:02 +00:00
|
|
|
|
|
|
|
Documents
|
|
|
|
---------
|
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
Data in MongoDB is represented (and stored) using JSON-style
|
|
|
|
documents. In mongoDB we use the *BsonDoc* type to represent these
|
|
|
|
documents. At the moment a *BsonDoc* is simply a tuple list of the
|
|
|
|
type '[(ByteString, BsonValue)]'. Here's a BsonDoc which could represent
|
|
|
|
a blog post:
|
|
|
|
|
|
|
|
> import Data.Time.Clock.POSIX
|
|
|
|
> now <- getPOSIXTime
|
|
|
|
> :{
|
|
|
|
let post = [(fromString "author", BsonString $ fromString "Mike"),
|
|
|
|
(fromString "text",
|
|
|
|
BsonString $ fromString "My first blog post!"),
|
|
|
|
(fromString "tags",
|
|
|
|
BsonArray [BsonString $ fromString "mongodb",
|
|
|
|
BsonString $ fromString "python",
|
|
|
|
BsonString $ fromString "pymongo"]),
|
|
|
|
(fromString "date", BsonDate now)]
|
|
|
|
:}
|
|
|
|
|
|
|
|
With all the type wrappers and string conversion, it's hard to see
|
|
|
|
what's actually going on. Fortunately the BSON library provides
|
|
|
|
conversion functions *toBson* and *fromBson* for converting native
|
|
|
|
between the wrapped BSON types and many native Haskell types. The
|
|
|
|
functions *toBsonDoc* and *fromBsonDoc* help convert from tuple lists
|
|
|
|
with plain *String* keys, or *Data.Map*.
|
|
|
|
|
|
|
|
Here's the same BSON data structure using these conversion functions:
|
|
|
|
|
|
|
|
> :{
|
|
|
|
let post = toBsonDoc [("author", toBson "Mike"),
|
|
|
|
("text", toBson "My first blog post!"),
|
|
|
|
("tags", toBson ["mongoDB", "Haskell"]),
|
|
|
|
("date", BsonDate now)]
|
|
|
|
:}
|
2010-02-28 12:19:02 +00:00
|
|
|
|
|
|
|
Inserting a Document
|
|
|
|
-------------------
|
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
> insert con testcol post
|
2010-02-28 12:19:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
Getting a single document with findOne
|
|
|
|
-------------------------------------
|
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
> findOne con testcol (toBsonDoc [("author", toBson "Mike")])
|
2010-02-28 12:19:02 +00:00
|
|
|
|
|
|
|
Querying for More Than One Document
|
|
|
|
------------------------------------
|
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
> cursor <- find con testcol (toBsonDoc [("author", toBson "Mike")])
|
2010-03-09 05:13:01 +00:00
|
|
|
> allDocs cursor
|
2010-02-28 12:19:02 +00:00
|
|
|
|
2010-03-01 14:28:38 +00:00
|
|
|
You can combine these into one line:
|
|
|
|
|
2010-03-10 00:32:36 +00:00
|
|
|
> docs <- allDocs =<< find con testcol (toBsonDoc [("author", toBson "Mike")])
|
2010-03-01 14:28:38 +00:00
|
|
|
|
|
|
|
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-10 00:32:36 +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
|