<p>After 2.5 years of development, version 1.0 of the <a href="https://cran.r-project.org/web/packages/mongolite/index.html">mongolite</a> package has been released to CRAN. The package is now stable, well documented, and will soon be submitted for peer review to be onboarded in the rOpenSci suite.</p>
<h2>MongoDB in R and mongolite</h2>
<p>I started working on mongolite in September 2014, and it was first announced at the rOpenSci <a href="https://twitter.com/_inundata/status/581605601882480640/photo/1">unconf 2015</a>. At this time, there were already two Mongo clients on CRAN: <a href="https://cran.r-project.org/web/packages/rmongodb/index.html">rmongodb</a> (no longer works) and <a href="https://cran.r-project.org/web/packages/RMongo/index.html">RMongo</a> (depends on Java). However I found both of them pretty clunky, and the MongoDB folks had just released 1.0 of their new C driver, so I decided to write a new client from scratch. </p>
<p>Mongolite aims to provide a simple R client for MongoDB, based on the excellent <a href="https://github.com/mongodb/mongo-c-driver">mongo-c-driver</a> combined with super-powers from the <a href="https://cran.r-project.org/web/packages/jsonlite/index.html">jsonlite</a> package. Simple means insert and query data in R using data-frames with a single command:</p>
<div><pre><code data-lang="r"><span></span><span># Create a connection</span>
con <span>&lt;-</span> mongolite<span>::</span>mongo<span>(</span><span>&quot;diamonds&quot;</span><span>,</span> 
  url <span>=</span> <span>&quot;mongodb://readwrite:test@ds043942.mongolab.com:43942/jeroen_test&quot;</span><span>)</span>

<span># Find diamonds with: cut == Premium &amp; price &lt; 500</span>
mydata <span>&lt;-</span> con<span>$</span>find<span>(</span><span>'{&quot;cut&quot; : &quot;Premium&quot;, &quot;price&quot; : { &quot;$lt&quot; : 500 } }'</span><span>)</span>
<span>print</span><span>(</span>mydata<span>)</span>
</code></pre></div>
<p>Running your own MongoDB server is easy. Either <a href="https://www.mongodb.com/download-center">download</a> it from the website or install it with your favorite package manager. To start the server simply run the <code>mongod</code> command (d for daemon) in a shell:</p>
<div><pre><code data-lang="text"><span></span># Install mongoDB server
brew install mongodb

# Run the server dameon
mongod
</code></pre></div>
<p>The <code>mongolite::mongo()</code> function wil default to the localhost server if no URI is specified. Try inserting and reading some data:</p>
<div><pre><code data-lang="r"><span></span><span># Create a connection</span>
con <span>&lt;-</span> mongolite<span>::</span>mongo<span>(</span><span>&quot;iris&quot;</span><span>)</span>

<span># Insert some data</span>
con<span>$</span>insert<span>(</span>datasets<span>::</span>iris<span>)</span>

<span># Count how much data is in the DB</span>
con<span>$</span>count<span>()</span>

<span># Read the data back</span>
con<span>$</span>find<span>(</span><span>'{}'</span><span>)</span>

<span># Wipe the collection</span>
con<span>$</span><span>drop</span><span>()</span>
</code></pre></div>
<p>In my experience, a simple interface is critical to get started. Obviously, advanced features are available in mongolite as well, but this will get you up to speed right way if you just need the data and get on with your job.</p>
<h2>Bookdown documentation</h2>
<p>The 1.0 release has fresh documentation based on the awesome bookdown system. You can find documentation on the mongolite <a href="https://jeroenooms.github.io/mongolite/">github homepage</a>.</p>
<p><a href="https://jeroenooms.github.io/mongolite/"></a></p>
<p>The bookdown is now the primary documentation source for mongolite.</p>
<h2>Why MongoDB</h2>
<p>MongoDB is the <a href="http://ropensci.org/feed.xml">most popular</a> nosql database (by market share), and the 5th most popular database allround. Mongo is relatively young in comparison with the traditional engines (Oracle, Microsoft, MySQL, Postgres), yet well established, fully open source, and backed by a professional company. </p>
<p>MongoDB provides a modern high-performance DB engine with cool features that cannot be found anywhere else. The high quality client drivers are a pleasure to work with, and actively maintained by professional engineers. Writing bindings, it quickly became obvious that Mongo does not suffer from the legacy bloat that I have come to associate with traditional DB engines.</p>
<p>At the same time the ecosystem is mature and offers reliability and continuity that makes it stand out from the proliferation of nosql systems. MongoDB has been widely adopted by users and distributions, so I am pretty confident it will still be around 5 or 10 years from now.</p>
<h2>Changes in 1.0</h2>
<p>The <a href="https://github.com/jeroenooms/mongolite/blob/master/NEWS">NEWS</a> file on Github lists what has changed in this release:</p>
<ul><li>New mongo bookdown docs at <a href="https://jeroenooms.github.io/mongolite">https://jeroenooms.github.io/mongolite</a></li>
<li>Update mongo-c-driver to upstream 1.6.0</li>
<li>Add basic decimal 128 support (coerce to double)</li>
<li>Improve enterprise authentication for LDAP, X509 and Kerberos</li>
<li>Windows: build with SSPI instead of SASL</li>
<li>Added <code>allow_invalid_hostname</code> parameter to <code>ssl_options()</code></li>
<li>Option <code>bigint_as_char</code> to parse int64 into string instead of double</li>
<li>New function <code>mongo_options()</code> to get/set global options</li>
<li>Function <code>mongo_log_level()</code> is removed (use <code>mongo_options()</code> instead)</li>
<li><code>insert()</code> now substitutes dots in key (column) names with underscores</li>
<li>Various fixes in <code>update()</code>, support for upsert</li>
<li>Add unit tests from bson spec (some tests fail in mongo-c-driver)</li>
<li>Switch to new C driver API mongoc_collection_find_with_opts()</li>
<li>Add R_registerRoutines() call to please CMD check</li>
</ul><h2>Tell us what you think</h2>
<p>At rOpenSci we're interested to hear who is using R and how. If you decide to give mongolite a try, please share your experiences and suggestions. Open an <a href="https://github.com/jeroenooms/mongolite/issues">issue on github</a> for specific problems and feature requests, or join the rOpenSci <a href="https://ropensci.slack.com/">slack</a> to talk about mongolite or other rOpenSci packages. Or just come say hi and hang out :)</p>
<p>We both love hearing from academic users, but also industry applications of R and synergy between the industry and open source scientific software.</p>