As always, we start with our dependencies. I used Ubuntu 18.04 and grabbed the mongodb binaries directly from the organization’s repository as the Ubuntu binaries were out of date.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6  
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list  
sudo apt-get update  

Then, I install mongo-db as well as some OpenSSL dependencies. This required me to make a directory at /data/db and give ownership of that directory to my user locally. Then, I had to run the server with root permissions because it opens a port.

sudo apt-get install -y mongodb-org  
sudo apt install libssl-dev libsasl2-dev  
sudo mkdir -p /data/db  
sudo chown username /data/db  
sudo mongod  

This one loads the mongo R library.

library('mongolite')

I decided to use the cars dataset because it was available when I opened the notebook. Below, we can see the cars data as a data frame.

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
df <- as.data.frame(cars)
df
##    speed dist
## 1      4    2
## 2      4   10
## 3      7    4
## 4      7   22
## 5      8   16
## 6      9   10
## 7     10   18
## 8     10   26
## 9     10   34
## 10    11   17
## 11    11   28
## 12    12   14
## 13    12   20
## 14    12   24
## 15    12   28
## 16    13   26
## 17    13   34
## 18    13   34
## 19    13   46
## 20    14   26
## 21    14   36
## 22    14   60
## 23    14   80
## 24    15   20
## 25    15   26
## 26    15   54
## 27    16   32
## 28    16   40
## 29    17   32
## 30    17   40
## 31    17   50
## 32    18   42
## 33    18   56
## 34    18   76
## 35    18   84
## 36    19   36
## 37    19   46
## 38    19   68
## 39    20   32
## 40    20   48
## 41    20   52
## 42    20   56
## 43    20   64
## 44    22   66
## 45    23   54
## 46    24   70
## 47    24   92
## 48    24   93
## 49    24  120
## 50    25   85

Here, I convert the dataframe into a Mongodb collection called “collection.”

collection <- mongo(collection = "df", db = "Cars")
collection$insert(df)
## List of 5
##  $ nInserted  : num 50
##  $ nMatched   : num 0
##  $ nRemoved   : num 0
##  $ nUpserted  : num 0
##  $ writeErrors: list()

Below shows how to call a single row and what the data looks like.

collection$iterate()$one()
## $type
## [1] "small"
## 
## $price
## [1] 15.9
## 
## $mpgCity
## [1] 25
## 
## $driveTrain
## [1] "front"
## 
## $passengers
## [1] 5
## 
## $weight
## [1] 2705

Advantages

Disadvantages