Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Haskell Data Analysis cookbook

You're reading from   Haskell Data Analysis cookbook Explore intuitive data analysis techniques and powerful machine learning methods using over 130 practical recipes

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781783286331
Length 334 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Nishant Shukla Nishant Shukla
Author Profile Icon Nishant Shukla
Nishant Shukla
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. The Hunt for Data FREE CHAPTER 2. Integrity and Inspection 3. The Science of Words 4. Data Hashing 5. The Dance with Trees 6. Graph Fundamentals 7. Statistics and Analysis 8. Clustering and Classification 9. Parallel and Concurrent Design 10. Real-time Data 11. Visualizing Data 12. Exporting and Presenting Index

Using MongoDB queries in Haskell

MongoDB is a nonrelational schemaless database. In this recipe, we will obtain all data from MongoDB into Haskell.

Getting ready

We need to install MongoDB on our local machine and have a database instance running in the background while we run the code in this recipe.

MongoDB installation instructions are located at http://www.mongodb.org. On Debian-based operating systems, we can use apt-get to install MongoDB, using the following command line:

$ sudo apt-get install mongodb

Run the database daemon by specifying the database file path as follows:

$ mkdir ~/db
$ mongod --dbpath ~/db

Fill up a "people" collection with dummy data as follows:

$ mongo
> db.people.insert( {first: "Joe", last: "Shmoe"} )

Install the MongoDB package from Cabal using the following command:

$ cabal install mongoDB

How to do it...

  1. Use the OverloadedString and ExtendedDefaultRules language extensions to make the MongoDB library easier to use:
    {-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-}
    import Database.MongoDB
  2. Define and implement main to set up a connection to the locally hosted database. Run MongoDB queries defined in the run function as follows:
    main :: IO ()
    main = do
        let db = "test"
        pipe <- runIOE $ connect (host "127.0.0.1")
        e <- access pipe master db run
        close pipe
        print e
  3. In run, we can combine multiple operations. For this recipe, run will only perform one task, that is, gather data from the "people" collection:
    run = getData
    
    getData = rest =<< find (select [] "people") {sort=[]}

How it works...

A pipe is established by the driver between the running program and the database. This allows running MongoDB operations to bridge the program with the database. The find function takes a query, which we construct by evoking the select :: Selector -> Collection -> aQueryOrSelection function.

Other functions can be found in the documentation at http://hackage.haskell.org/package/mongoDB/docs/Database-MongoDB-Query.html.

See also

If the MongoDB database is on a remote server, refer to the Reading from a remote MongoDB server recipe to set up a connection with remote databases.

You have been reading a chapter from
Haskell Data Analysis cookbook
Published in: Jun 2014
Publisher:
ISBN-13: 9781783286331
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime