Read movie data from PostgresSQL DB

driver <- dbDriver("PostgreSQL")
con <- dbConnect(driver, host="localhost", port=5432, dbname="movies", user="postgres")
query <- "select m.mid, m.title, m.director, re.name, ra.stars from movie m inner join rating ra on m.mid = ra.mid inner join reviewer re on
ra.rid = re.rid"
rs <- dbSendQuery(con, query)
movies <- dbFetch(rs, n=-1)
head(movies)
##   mid        title          director           name stars
## 1 106        LOGAN     James Mangold Sarah Martinez     4
## 2 103      DUNKIRK Christopher Nolan Sarah Martinez     1
## 3 101      GET OUT      Jordan Peele Sarah Martinez     5
## 4 105    LADY BIRD      Greta Gerwig Sarah Martinez     4
## 5 102 THE BIG SICK Michael Showalter Sarah Martinez     5
## 6 104 WONDER WOMAN     Patty Jenkins Sarah Martinez     2

How to install MongoDB on mac OS?

Using home brew
brew install mongodb
mongod # to start mongodb

Write data to MongoDB

movies_collection = mongo(collection = "movies", db = "movies_db")
movies_collection$insert(movies)
## List of 5
##  $ nInserted  : num 48
##  $ nMatched   : num 0
##  $ nRemoved   : num 0
##  $ nUpserted  : num 0
##  $ writeErrors: list()

Verify count of rows from both databases

nrow(movies)
## [1] 48
movies_collection$count()
## [1] 48

Get the first record from mongo db

unlist(movies_collection$iterate()$one())
##              mid            title         director             name 
##            "106"          "LOGAN" " James Mangold" "Sarah Martinez" 
##            stars 
##              "4"

Relational vs. NoSQL database

Advantages

Disadvantages