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.
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 mongolite user manual provides a great introduction with details and worked examples.
GridFS Support!
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.
We support two interfaces for sending/receiving data from/to GridFS. The fs$read() and fs$write() 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.
# Assume 'mongod' is running on localhost
fs 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 year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight
#__ #__ 1 2013 1 1 517 515 2 830 819 11 UA 1545
#__ 2 2013 1 1 533 529 4 850 830 20 UA 1714
#__ 3 2013 1 1 542 540 2 923 850 33 AA 1141
#__ 4 2013 1 1 544 545 -1 1004 1022 -18 B6 725
#__ 5 2013 1 1 554 600 -6 812 837 -25 DL 461
#__ 6 2013 1 1 554 558 -4 740 728 12 UA 1696
#__ # ... with 8 more variables: tailnum , origin , dest , air_time , distance ,
#__ # hour , minute , time_hour The fs$upload() and fs$download() methods on the other hand copy directly between GridFS and your local disk. This API is vectorized so it can transfer many files at once. Hover over to the gridfs chapter in the manual for more examples.
Running Commands
MongoDB exposes an enormous number of database commands, and mongolite cannot provide wrappers for each command. As a compromise, the new version of mongolite exposes an api to run raw commands, so you can manually run the commands for which we do not expose wrappers.
The result data from the commannd automatically gets simplified into nice R data structures using the jsonlite simplification system (but you can set simplify = FALSE if you prefer json structures).
m $ok
#__ [1] 1
For example we can run the listDatabases command to list all db’s on the server:
admin $databases
#__ name sizeOnDisk empty
#__ 1 admin 32768 FALSE
#__ 2 config 73728 FALSE
#__ 3 local 73728 FALSE
#__ 4 test 72740864 FALSE
#__ #__ $totalSize
#__ [1] 72921088
#__ #__ $ok
#__ [1] 1
The server tools chapter in the manual has some more examples.
Connection Pooling
Finally another much requested feature was connection pooling. Previously, mongolite would open a new database connection for each collection object in R. However as of version 2.0, mongolite will use existing connections to the same database when possible.
# This will use a single database connection
db.flights 1 5b70a37a47a302506117c352 flights 45112028 2018-08-12 23:15:38