The task is,

  1. Write the contents of a json file of your choosing into a mongodb database, then
  2. Use R to display some of the information from the mongodb database.

library(rjson)
library(rmongodb)
library(knitr)

Before I did R coding, I made a database in MongoDB by importing “new york jazz perfomance” data using “mongoimport”.

Among the collections above, the data I want is “nyjazz” that has information about jazz musicians, addresses, etc.

mongo <- mongo.create()
mongo
## [1] 0
## attr(,"mongo")
## <pointer: 0x7f9a49d57780>
## attr(,"class")
## [1] "mongo"
## attr(,"host")
## [1] "127.0.0.1"
## attr(,"name")
## [1] ""
## attr(,"username")
## [1] ""
## attr(,"password")
## [1] ""
## attr(,"db")
## [1] "admin"
## attr(,"timeout")
## [1] 0
mongo.is.connected(mongo)
## [1] TRUE
if(mongo.is.connected(mongo) == TRUE) {
        mongo.get.databases(mongo)
}
## [1] "mydb" "test"
if(mongo.is.connected(mongo) == TRUE) {
        db <- "mydb"
        mongo.get.database.collections(mongo, db)
}
## [1] "mydb.testData" "mydb.nyjazz"   "mydb.nyjazz2"

I changed the bson to list, and then changed it to a data frame.

coll <- "mydb.nyjazz"
tmp <-  mongo.find.one(mongo,coll)
class(tmp)
## [1] "mongo.bson"
mgdata <- mongo.bson.to.list(tmp)
df <- data.frame(musician=mgdata[[2]], place=mgdata[[3]], address=mgdata[[4]], time=mgdata[[5]], stringsAsFactors = FALSE)
str(df)
## 'data.frame':    20 obs. of  4 variables:
##  $ musician: chr  "Henry Butler " "Thar " "Julian Lage Trio " "Kirk Knuffke Trio " ...
##  $ place   : chr  "Minton's" "Cornelia Street Café" "Jazz Standard" "Cornelia Street Café" ...
##  $ address : chr  "206 West 118th Street" "29 Cornelia Street" "116 East 27th Street" "29 Cornelia Street" ...
##  $ time    : chr  "April 9 and 16 at 7:30 and 9:30 p.m." "April 15 at 8:30 p.m." "April 14-15 at 7:30 and 10 p.m." "April 16 at 8:30 p.m." ...

Searching jazz performances in Lincon center

kable(df[grep('Center', df$place),])
musician place address time
5 ‘Celebrating Joe Temperley: From Duke to the JLCO’ Rose Theater, Frederick P. Rose Hall, Jazz at Lincoln Center Broadway April 16-18 at 8 p.m.
15 Samba Jazz and the Music of Jobim Dizzy’s Club Coca-Cola, Frederick P. Rose Hall, Jazz at Lincoln Center Broadway April 16-19 at 7:30 and 9:30 p.m.
19 National Endowment for the Arts Jazz Master Ceremony Rose Theater, Frederick P. Rose Hall, Jazz at Lincoln Center Broadway April 20 at 7:30 p.m.