Monday, June 24, 2013

How to query MongoDB from R?

At my little start-up we use Mongodb for stats and this is awesome for me. I’m quite new to Mongo though when i worked at Google Inc. i used something called Big table that kind of works the same. As you might have noticed i’m highly using R for all my analyses and that’s how i want to keep it.  


I recently came across RMongo, a database access layer to MongoDB in R as an R package. I’m assuming that you already have R and Mongo installed otherwise you might want to go through my previous tutorials on installations of R or Mongo.


To install RMongo:
>install.packages(“RMongo”)
If that does not work, try downloading it from http://cran.r-project.org/web/packages/RMongo/index.html and run:
install.packages("~/Downloads/RMongo_XX.XX.XX.tar.gz", repos=NULL, type="source")

The Data Querying?

Now from within R, this is how to connect to a local MongoDB. For those with remote databases please know that the mongoDbConnect function takes some additional arguments . 

Run the command below to know these arguments and what they mean.


> library("RMongo")

>?mongoDbConnect()
and you will be presented with a number of various arguments to use.
If you are new to Mongo you might want to go through this wonderful tutorial by nettuts here title “Getting started with mongodb”. This tutorial is awesome though got some few typos!


I did put my database into a test db, so this is how i connect to the db called "test"
> the_mongo  <- mongoDbConnect("test")


Also run
>?dbGetQuery()

> the_result <- dbGetQuery(the_mongo, "nettuts", "", 0, 10)

> the_result


It’s also possible to use more complex queries to extract data.
The main query function takes five arguments as shown by the:

>?dbGetQuery()

  • database connection
  • collection name
  • query
  • skip - how many objects to skip
  • limit - total number of objects to return
For example, extract all fields from the “nettuts” collection where age is greater than or equal to 47:
> some_result <- dbGetQuery(the_mongo, "nettuts", "{'age' : { '$gte' : 47}}")

No comments:

Post a Comment

Add any comments if it helped :)