RMongo: Accessing MongoDB in R

November 3, 2010
By

(This article was first published on Tommy Chheng » R, and kindly contributed to R-bloggers)

I recently created RMongo, a database access layer to MongoDB in R as an R package.

To install RMongo, download it from https://github.com/quid/RMongo/downloads
Run:
R CMD install RMongo_0.0.17.tar.gz

I tried to mimic the RMySQL commands in RMongo. Below are some example commands.
library(RMongo)

#ask for help
?RMongo

#connect to a database
mongo <- mongoDbConnect("eat2treat_development")

#show the collections
dbShowCollections(mongo)
[1] "conditions" "users" "nutrient_metadatas" "system.indexes" "user_food_preferences" "food_metadatas"
[7] "food_group_ratings

# perform an 'all' query with a document limit of 2 and offset of 0.
# the results is a data.frame object. Nested documents are not supported at the moment. They will just be the string output.
> results <- dbGetQuery(mongo, "nutrient_metadatas", "{}", 0, 2)
> names(results)
[1] "X_id" "name" "nutrient_definition_id" "description"
> results
X_id name nutrient_definition_id
1 4cd0f8e31e627d4e6600000e Adjusted Protein 257
2 4cd0f9061e627d4e6600001a Sodium 307

> results <- dbGetQuery(mongo, "nutrient_metadatas", '{"nutrient_definition_id": 307}')
> results
X_id name nutrient_definition_id
1 4cd0f9061e627d4e6600001a Sodium 307

> dbDisconnect(mongo)

RMongo is very alpha at this point. I built it as a quick way to prototype algorithms with data from mongoDB in R. Most of RMongo uses the mongo-java-driver to perform json-formatted queries. The R code in the package uses rJava to communicate with the mongo-java-driver.

Please report any bugs or necessary improvements. Or better yet, send in pull requests via the RMongo github project page!

To leave a comment for the author, please follow the link and comment on his blog: Tommy Chheng » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.