Chapter 1 Getting Started

This book provides a high level introduction to using MongoDB with the mongolite client in R.

1.1 Requirements (Linux / Mac)

Installation from source on Linux requires openssl and Cyrus SASL (not GNU sasl). On Debian or Ubuntu use libssl-dev and libsasl2-dev:

sudo apt-get install -y libssl-dev libsasl2-dev

On Fedora, CentOS or RHEL use openssl-devel and cyrus-sasl-devel:

sudo yum install openssl-devel cyrus-sasl-devel

On OS-X sasl is included with the system so only openssl is needed.

brew install openssl

On Windows all dependencies are statically linked with mongolite; no separate installation is required. Elsewhere, mongolite will automatically find the system libraries when installed in default locations.

1.2 Install mongolite in R

Binary packages of mongolite for OS-X or Windows can be installed directly from CRAN:

install.packages("mongolite")

Alternatively you can install the development version, which contains the latest features and bugs. This required compiling from source:

# installs development version of 'mongolite'
devtools::install_github("jeroenooms/mongolite")

Note that Windows users need to install the Rtools toolchain in order to compile from source. This is not needed for the CRAN version above.

1.3 Run local mongod

Please refer to the official MongoDB install manual for instructions how to setup a local MongoDB server. To get started on MacOS, simply use:

brew install mongodb

The Homebrew package does not install any system services. To run the daemon in a console:

mongod

Use this for running examples as the mongolite package defaults to url = "mongodb://localhost".

1.4 Testing with SSL

To run a local mongod with SSL support you need a SSL key and certificate. See the Mongo Configure SSL manual page. A generate a self signed cert for testing purposes:

cd /etc/ssl/
openssl req -newkey rsa:2048 -new -x509 -days 365 -nodes -out mongodb-cert.crt -keyout mongodb-cert.key
cat mongodb-cert.key mongodb-cert.crt > mongodb.pem

And then start the daemon with:

mongod --sslMode requireSSL --sslPEMKeyFile /etc/ssl/mongodb.pem

Because this certificate has not been signed with a CA, you need to set weak_cert_validation in the client to connect:

m <- mongo(url = "mongodb://localhost?ssl=true", options = ssl_options(weak_cert_validation = T))

Obviously in production you need to get your cert signed by a CA instead.