movies and ratings tables, and then populated them with the movies and ratings data from Week 2 homework. I then created an account on mongodb cloud. I then created a collection cunygraph and a database movies and then populated it with the data from the MySQL database.#mongodb connection string
mongoConnStr <- paste("mongodb+srv://", mongoCredentials, mongoConn, sep = "")
#mySql connection
mySqlConnStr <- dbConnect(RMySQL::MySQL(), dbname = mySqlDbName, host="remotemysql.com", port=3306, user = mySqlUser, password = mySqlPassword)
#Mongocollection
mongoCollection <- mongo(collection = mongoCollection, db = mongoDbName, url = mongoConnStr, verbose = FALSE, options = ssl_options())
dbListTables(mySqlConnStr)
## [1] "movie" "ratings"
moviesQuery <- fetch(dbSendQuery(mySqlConnStr, "SELECT * FROM movie m
LEFT JOIN (SELECT ratedby, movieid, date, rating, `desc` FROM ratings r) r
ON m.id = r.movieid
order by m.id
"))
## Warning in .local(conn, statement, ...): Decimal MySQL column 10 imported
## as numeric
# necessary because the LEFT JOIN pulls the parents and children and make the parent appear duplicated based on the number of children
movies <-distinct(moviesQuery, id, poster, title, releasedate, genre, details, summary)
# for index table
mv <- data.frame(c(movies$title), c(movies$releasedate), c(movies$genre), c(movies$details))
datatable(mv, colnames= c("Movie", "Release Date", "Genre", "Details"), class = 'cell-border stripe', options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#26868d', 'color': '#fff', 'text-align': 'center !important'});",
"$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
"}")
))
mongoCollection created earlier to insert the data into the mongodbmongoCollection$insert(mv)
List of 5 $ nInserted : num 6 $ nMatched : num 0 $ nRemoved : num 0 $ nUpserted : num 0 $ writeErrors: list()
mongoCollection$count()
## [1] 24
mongoMovies = mongoCollection$find('{}')
mongoCollection with Datatabledatatable(mongoMovies, colnames= c("Movie", "Release Date", "Genre", "Details"), class = 'cell-border stripe', options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#26868d', 'color': '#fff', 'text-align': 'center !important'});",
"$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
"}")
))