<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/mongolite-2-0-gridfs-connection-pooling-and-more/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/technotes/2018/08/14/mongolite-20/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This week version 2.0 of the mongolite package has been released to CRAN. Major new features in this release include support for MongoDB 4.0, GridFS, running database commands, and connection pooling.</p>
<p>Mongolite is primarily an easy-to-use client to get data in and out of MongoDB. However it supports increasingly many advanced features like aggregation, indexing, map-reduce, streaming, encryption, and enterprise authentication. The <a rel="nofollow" href="https://jeroen.github.io/mongolite/" target="_blank">mongolite user manual</a> provides a great introduction with details and worked examples.</p>
<h2>GridFS Support!</h2>
<p>New in version 2.0 is support for the MongoDB GridFS system. A GridFS is a special type of Mongo collection for storing binary data, such as files. To the user, a GridFS looks like a key-value server with potentially very large values.</p>
<p>We support two interfaces for sending/receiving data from/to GridFS. The <code>fs$read()</code> and <code>fs$write()</code> methods are the most flexible and can stream data from/to an R connection, such as a file, socket or url. These methods support a progress counter and can be interrupted if needed. These methods are recommended for reading or writing single files.</p>
<pre><code># Assume 'mongod' is running on localhost
fs &lt;- gridfs()
buf &lt;- serialize(nycflights13::flights, NULL)
fs$write(buf, 'flights')
#> [flights]: written 45.11 MB (done)
#>  List of 6
#> $ id      : chr &quot;5b70a37a47a302506117c352&quot;
#> $ name    : chr &quot;flights&quot;
#> $ size    : num 45112028
#> $ date    : POSIXct[1:1], format: &quot;2018-08-12 23:15:38&quot;
#> $ type    : chr NA
#> $ metadata: chr NA

# Read serialized data:
out &lt;- fs$read('flights')
flights &lt;- unserialize(out$data)
# [flights]: read 45.11 MB (done)
#>  A tibble: 6 x 19
#>    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight
#>   </code></pre>