RMongo: Accessing MongoDB in R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.