library(odbc)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(dbplyr)
## 
## Attaching package: 'dbplyr'
## The following objects are masked from 'package:dplyr':
## 
##     ident, sql

We pulling the data down from mySQL:

db <- DBI::dbConnect(odbc::odbc(), "SQL")
movie <- as.data.frame(db %>% tbl("movie"))
movie
##   movie_id                movie_name movie_genre
## 1        1                   The Nun      Terror
## 2        2                Peppermint   Adventure
## 3        3 God Bless the Broken Road      Action
## 4        4            The Apparition      Action
## 5        5                   The Meg      Action
## 6        6                   Mile 22      Action
rating <- as.data.frame(db %>% tbl("rating"))
rating
##   rating_id user_id movie_id movie_rating
## 1         1       1        1            4
## 2         2       2        2            4
## 3         3       3        3            4
## 4         4       4        4            4
## 5         5       5        5            5
## 6         6       6        6            3
merge(movie,rating, by = c('movie_id'), all.x=TRUE)
##   movie_id                movie_name movie_genre rating_id user_id
## 1        1                   The Nun      Terror         1       1
## 2        2                Peppermint   Adventure         2       2
## 3        3 God Bless the Broken Road      Action         3       3
## 4        4            The Apparition      Action         4       4
## 5        5                   The Meg      Action         5       5
## 6        6                   Mile 22      Action         6       6
##   movie_rating
## 1            4
## 2            4
## 3            4
## 4            4
## 5            5
## 6            3

Loading Data Into MongoDB

Now to load the data into a MongoDB. First we need a connection:

library(mongolite)
mdb_movie = mongo(collection = "movie", db = "movie")
library(mongolite)
mdb_rating = mongo(collection = "rating", db = "rating")

Now we can load the collection name movie into the MongoDB.

mdb_movie$insert(movie)
## List of 5
##  $ nInserted  : num 6
##  $ nMatched   : num 0
##  $ nRemoved   : num 0
##  $ nUpserted  : num 0
##  $ writeErrors: list()
mdb_rating$insert(rating)
## List of 5
##  $ nInserted  : num 6
##  $ nMatched   : num 0
##  $ nRemoved   : num 0
##  $ nUpserted  : num 0
##  $ writeErrors: list()

Now we run a print out of the database to make sure the data was uploaded.

alldata_movie <- mdb_movie$find('{}')
head(alldata_movie)
##   movie_id                movie_name movie_genre
## 1        1                   The Nun      Terror
## 2        2                Peppermint   Adventure
## 3        3 God Bless the Broken Road      Action
## 4        4            The Apparition      Action
## 5        5                   The Meg      Action
## 6        6                   Mile 22      Action
alldata_rating <- mdb_rating$find('{}')
head(alldata_rating)
##   rating_id user_id movie_id movie_rating
## 1         1       1        1            4
## 2         2       2        2            4
## 3         3       3        3            4
## 4         4       4        4            4
## 5         5       5        5            5
## 6         6       6        6            3
merge(head(alldata_movie),head(alldata_rating), by = c('movie_id'), all=TRUE)
##   movie_id                movie_name movie_genre rating_id user_id
## 1        1                   The Nun      Terror         1       1
## 2        2                Peppermint   Adventure         2       2
## 3        3 God Bless the Broken Road      Action         3       3
## 4        4            The Apparition      Action         4       4
## 5        5                   The Meg      Action         5       5
## 6        6                   Mile 22      Action         6       6
##   movie_rating
## 1            4
## 2            4
## 3            4
## 4            4
## 5            5
## 6            3
The migration from MySQL to MongoDB is complete.

Advantages and Disadvantages of storing the data in a relational database vs. your NoSQL database.

Advantages of using a relational database: is that you can reduce data integrity and eliminate a lot of anomolies.
Disadvantages of NoSQL:store is easier to retrieve and the data access is faster as the data but need to consider YOUR use case more clearly before you commit to much to a system.