New R Driver Option in MongoDB 3.6
Tags: mongodb, rlanguage, 36
Published: 2018-07-07 00:27:06

---
title: "New R Driver Option in MongoDB 3.6"
published: true
tags: MongoDB,R Language,3.6
canonical_url: https://www.kenwalger.com/blog/nosql/mongodb/new-r-driver-option-mongodb-3-6/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/0oi1z0d9g4bcqgcfrq46.png
---

In a [previous post](https://dev.to/kenwalger/using-r-with-mongodb-25g),&nbsp;I discussed some options for using [MongoDB](https://www.mongodb.com) with the R Language. While the information in that post is still accurate, MongoDB 3.6 introduces a new R driver option. Unlike the drivers previously discussed, this new R driver is under active development.

### New R Driver

The new R driver, mongolite, can be found on [GitHub](https://github.com/jeroen/mongolite) and is easily installed from the [Comprehensive R Archive Network](https://cran.r-project.org/web/packages/mongolite/index.html) (CRAN) using the `install.packages("mongolite")` command&nbsp;on Windows or OS-X. The driver, or client, is authored by Jeroen Ooms who also has provided some [nice documentation](https://cran.r-project.org/web/packages/mongolite/mongolite.pdf) in PDF format. Let’s head into&nbsp;[RStudio](https://www.rstudio.com) and take a look at using the new R driver.

### Accessing MongoDB

Let’s use the same [dataset](http://kenwalger.com/public_html/assets/sample-data.zip) as in the previous post and, with [mongoimport](https://www.kenwalger.com/blog/nosql/mongodb/importing-data-mongoimport/), get the data into a MongoDB [collection](https://docs.mongodb.com/manual/core/databases-and-collections/). The database again will be called `kenblog` and the collection is `scores`. Here again is a sample document in the collection:

```
{
   "_id" : ObjectId("5627207b33ff2cf40effc25e"),
   "student" : 2,
   "type" : "quiz",
   "score" : 74
}
```

After using the `install.packages("mongolite")` command, we can put the power of R to work. We establish a connection to our database:

```
> require("mongolite")
Loading required package: mongolite
> connection <- mongo(collection = "scores", db = "kenblog", url = "mongodb://localhost")
```

The mongo&nbsp;connection method accepts the following arguments:

- collection
- db
- url
- verbose
- options

The `collection` and `db`&nbsp;arguments allow for the specification of the names of the respective database information. `url`&nbsp;is the mongo connection string in URI format. If you need additional output from the connection one can set the `verbose` boolean value to TRUE. Additional connection `options`, such as SSL information, can also be passed in.

With a connection established, let’s query our `scores` collection for `exam` data.

```
examQuery <- connection$find('{"type": "exam"}')
```

This brings in our 585 `exam` documents. We can then create a vector of the exam scores and have a look at their summary.

```
> exam_scores <- examQuery[c('score')] 
> summary(exam_scores)
     score       
 Min. : 60.00  
 1st Qu.: 72.00  
 Median : 79.00  
 Mean : 79.45  
 3rd Qu.: 86.00  
 Max. :100.00
```

Personally, I’m already liking the syntax of this new R driver for doing queries and working with MongoDB in R.

Other methods that will be familiar to MongoDB users are `drop()` to drop a collection, `aggregate` for [aggregation pipeline](https://docs.mongodb.com/manual/core/aggregation-pipeline/index.html) operations,&nbsp;and `insert` for creating information in the database. There are many additional methods that can be used that allow for map-reduce operations and importing or exporting JSON or BSON data, and many more.

Further, with support for features such as indexing, encryption, and authentication, this new R driver is much more robust than previous options.

### Wrap Up

Connecting to MongoDB from R is pretty straightforward and simple using the new R driver. It is the new “official” and supported method to leverage the power of R with the flexibility and power of MongoDB.

R is a great statistical language and can definitely be used to query and analyze MongoDB collections. If you are using R in your work today, this new way of connecting with MongoDB is definitely worth a look.

* * *

Follow me on Twitter [@kenwalger](https://www.twitter.com/kenwalger) to get the latest updates on my postings.

There are a few MongoDB specific terms in this post. I created a [MongoDB Dictionary](https://www.echoskillstore.com/MongoDB-Dictionary/45103) skill for the [Amazon Echo](https://www.amazon.com/gp/product/B01DFKC2SO/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01DFKC2SO&linkCode=as2&tag=kenwalgersite-20&linkId=f9e513223de2525a72b95cf9561db55b)&nbsp;line of products. Check it out and you can say “Alexa, ask MongoDB for the definition of a&nbsp;document?” and get a helpful response.

[![Facebook](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png?resize=48%2C48&ssl=1 "Share on Facebook")](http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F&t=New%20R%20Driver%20Option%20in%20MongoDB%203.6&s=100&p%5Burl%5D=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F&p%5Bimages%5D%5B0%5D=https%3A%2F%2Fi1.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F10%2Ffeature.png%3Ffit%3D125%252C125%26ssl%3D1&p%5Btitle%5D=New%20R%20Driver%20Option%20in%20MongoDB%203.6 "Share on Facebook")[![twitter](https://i0.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png?resize=48%2C48&ssl=1 "Share on Twitter")](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F&text=Hey%20check%20this%20out "Share on Twitter")[![google_plus](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png?resize=48%2C48&ssl=1 "Share on Google+")](https://plus.google.com/share?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F "Share on Google+")[![reddit](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png?resize=48%2C48&ssl=1 "Share on Reddit")](http://www.reddit.com/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F&title=New%20R%20Driver%20Option%20in%20MongoDB%203.6 "Share on Reddit")[![linkedin](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png?resize=48%2C48&ssl=1 "Share on Linkedin")](http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F&title=New%20R%20Driver%20Option%20in%20MongoDB%203.6 "Share on Linkedin")[![mail](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png?resize=48%2C48&ssl=1 "Share by email")](mailto:?subject=New%20R%20Driver%20Option%20in%20MongoDB%203.6&body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fnosql%2Fmongodb%2Fnew-r-driver-option-mongodb-3-6%2F "Share by email")

The post [New R Driver Option in MongoDB 3.6](https://www.kenwalger.com/blog/nosql/mongodb/new-r-driver-option-mongodb-3-6/) appeared first on [Blog of Ken W. Alger](https://www.kenwalger.com/blog).