{"id":743,"date":"2018-01-03T19:33:16","date_gmt":"2018-01-04T03:33:16","guid":{"rendered":"https:\/\/www.kenwalger.com\/blog\/?p=743"},"modified":"2020-01-05T17:46:41","modified_gmt":"2020-01-06T01:46:41","slug":"new-r-driver-option-mongodb-3-6","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/new-r-driver-option-mongodb-3-6\/","title":{"rendered":"New R Driver Option in MongoDB 3.6"},"content":{"rendered":"<p>In a <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/using-r-mongodb\/\">previous post<\/a>,\u00a0I discussed some options for using <a href=\"https:\/\/www.mongodb.com\">MongoDB<\/a> 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.<\/p>\n<h3>New R Driver<\/h3>\n<p>The new R driver, mongolite, can be found on <a href=\"https:\/\/github.com\/jeroen\/mongolite\">GitHub<\/a> and is easily installed from the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/mongolite\/\">Comprehensive R Archive Network<\/a> (CRAN) using the <code>install.packages(\"mongolite\")<\/code> command\u00a0on Windows or OS-X. The driver, or client, is authored by Jeroen Ooms who also has provided some <a href=\"https:\/\/cran.r-project.org\/web\/packages\/mongolite\/mongolite.pdf\">nice documentation<\/a> in PDF format. Let&#8217;s head into\u00a0<a href=\"https:\/\/www.rstudio.com\">RStudio<\/a> and take a look at using the new R driver.<\/p>\n<h3>Accessing MongoDB<\/h3>\n<p>Let&#8217;s use the same <a href=\"https:\/\/github.com\/kenwalger\/datasets\/tree\/master\/r-driver-blog\">dataset<\/a> as in the previous post and, with <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/importing-data-mongoimport\/\">mongoimport<\/a>, get the data into a MongoDB <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/databases-and-collections\/\">collection<\/a>. The database again will be called <code>kenblog<\/code> and the collection is <code>scores<\/code>. Here again is a sample document in the collection:<\/p>\n<pre>{\n   \"_id\" : ObjectId(\"5627207b33ff2cf40effc25e\"),\n   \"student\" : 2,\n   \"type\" : \"quiz\",\n   \"score\" : 74\n}\n<\/pre>\n<p>After using the <code>install.packages(\"mongolite\")<\/code> command, we can put the power of R to work. We establish a connection to our database:<\/p>\n<pre>&gt; require(\"mongolite\")\n<span style=\"color: #ff0000;\">Loading required package: mongolite<\/span>\n> connection &lt;- mongo(collection = \"scores\", db = \"kenblog\", url = \"mongodb:\/\/localhost\")\n<\/pre>\n<p>The mongo\u00a0connection method accepts the following arguments:<\/p>\n<ul>\n<li>collection<\/li>\n<li>db<\/li>\n<li>url<\/li>\n<li>verbose<\/li>\n<li>options<\/li>\n<\/ul>\n<p>The <code>collection<\/code> and <code>db<\/code>\u00a0arguments allow for the specification of the names of the respective database information. <code>url<\/code>\u00a0is the mongo connection string in URI format. If you need additional output from the connection one can set the <code>verbose<\/code> boolean value to TRUE. Additional connection <code>options<\/code>, such as SSL information, can also be passed in.<\/p>\n<p>With a connection established, let&#8217;s query our <code>scores<\/code> collection for <code>exam<\/code> data.<\/p>\n<pre>examQuery &lt;- connection$find('{\"type\": \"exam\"}')\n<\/pre>\n<p>This brings in our 585 <code>exam<\/code> documents. We can then create a vector of the exam scores and have a look at their summary.<\/p>\n<pre>&gt; exam_scores &lt;- examQuery[c('score')] \n> summary(exam_scores)\n     score       \n Min.   : 60.00  \n 1st Qu.: 72.00  \n Median : 79.00  \n Mean   : 79.45  \n 3rd Qu.: 86.00  \n Max.   :100.00 \n<\/pre>\n<p>Personally, I&#8217;m already liking the syntax of this new R driver for doing queries and working with MongoDB in R.<\/p>\n<p>Other methods that will be familiar to MongoDB users are <code>drop()<\/code> to drop a collection, <code>aggregate<\/code> for <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/aggregation-pipeline\/index.html\">aggregation pipeline<\/a> operations,\u00a0and <code>insert<\/code> 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.<\/p>\n<p>Further, with support for features such as indexing, encryption, and authentication, this new R driver is much more robust than previous options.<\/p>\n<h3>Wrap Up<\/h3>\n<p>Connecting to MongoDB from R is pretty straightforward and simple using the new R driver. It is the new &#8220;official&#8221; and supported method to leverage the power of R with the flexibility and power of MongoDB.<\/p>\n<p>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. If R is still new to you, <a href=\"https:\/\/www.amazon.com\/gp\/product\/1449357105\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1449357105&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=b895e16ffdd986b0fbf13f68a20a0622\" target=\"_blank\" rel=\"noopener noreferrer\">Learning R: A Step-by-Step Function Guide to Data Analysis<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=1449357105\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> is a great way to get started. Or the <a href=\"https:\/\/www.amazon.com\/gp\/product\/1119055806\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1119055806&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=6b2205ff992215cd6d34217fa75b9f49\" target=\"_blank\" rel=\"noopener noreferrer\">R For Dummies<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=1119055806\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> book may be of interest as well.<\/p>\n<p><em>This post was updated on 5 Jan 2020.<\/em><\/p>\n<hr \/>\n<p>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\">@kenwalger<\/a> to get the latest updates on my postings.<\/p>\n<p>There are a few MongoDB specific terms in this post. I created a <a href=\"https:\/\/www.echoskillstore.com\/MongoDB-Dictionary\/45103\">MongoDB Dictionary<\/a> skill for the <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01DFKC2SO\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01DFKC2SO&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=f9e513223de2525a72b95cf9561db55b\" rel=\"noopener noreferrer\">Amazon Echo<\/a>\u00a0line of products. Check it out and you can say &#8220;Alexa, ask MongoDB for the definition of a\u00a0document?&#8221; and get a helpful response.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F743&amp;t=New%20R%20Driver%20Option%20in%20MongoDB%203.6&amp;s=100&amp;p[url]=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F743&amp;p[images][0]=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F10%2Ffeature.png&amp;p[title]=New%20R%20Driver%20Option%20in%20MongoDB%203.6\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F743&amp;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F743&amp;title=New%20R%20Driver%20Option%20in%20MongoDB%203.6\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&amp;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F743&amp;title=New%20R%20Driver%20Option%20in%20MongoDB%203.6\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=New%20R%20Driver%20Option%20in%20MongoDB%203.6&amp;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F743\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>In a previous post,\u00a0I discussed some options for using MongoDB 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 &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/new-r-driver-option-mongodb-3-6\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;New R Driver Option in MongoDB 3.6&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":654,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[4,1188],"tags":[1102],"yst_prominent_words":[117,253,99,104,1091,389,756,1203,87,1191,1189,103,722,405,1199,1196,1190,385,1195,1194],"class_list":["post-743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","category-r-language","tag-3-6","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/10\/feature.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-bZ","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":12,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/743\/revisions"}],"predecessor-version":[{"id":1133,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/743\/revisions\/1133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/654"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=743"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}