require(RMySQL)
## Loading required package: RMySQL
## Loading required package: DBI
require(mongolite)
## Loading required package: mongolite

###Getting Data from MySQL

dbuser <- 'data607'
dbpassword <- 'password1!'
db_name <- 'breweries'
db_table <- 'breweries_us'
db_host <- '127.0.0.1'
db_port <- 3306
mydb <-  dbConnect(MySQL(), user = dbuser, password = dbpassword, dbname = db_name, host = db_host, port = db_port)
rs = dbSendQuery(mydb, "select * from breweries_us")
mysql_data = fetch(rs, n=-1)

###Transforming into NoSQL Mongodb

my_collection = mongo(collection = "breweries_us", db = "breweries")
my_collection$insert(mysql_data)
## List of 5
##  $ nInserted  : num 2406
##  $ nMatched   : num 0
##  $ nRemoved   : num 0
##  $ nUpserted  : num 0
##  $ writeErrors: list()
my_collection$count()
## [1] 4812
my_collection$iterate()$one()
## $brewery_name
## [1] "Valley Brewing Co."
## 
## $type
## [1] "Brewpub"
## 
## $address
## [1] "PO Box 4653, Stockton, California, 95204"
## 
## $website
## [1] "http://www.valleybrew.com/"
## 
## $state
## [1] "california"
## 
## $state_breweries
## [1] 284

A NoSQL database has more flexibility than relational databases since there isn’t requirements in terms of defining relationships and other things like keys. NoSQL would be ideal for a “code first approach” when building an application. Relational databases would be ideal for “data first” approaches because of the schema mapping in involves upfront. In addition, relational databases support data integrity more than NoSQL databases.