MongoDB with R

Annisarahmi

11/17/2021

Persiapan

Library yang digunakan untuk menggunakan MongDB di R adalah mongolite

library(mongolite)

Selanjutnya akan dibuat koneksi ke database yang disimpan dalam object connection_string.

Selanjutnya akan digunakan collection sales sebagai database untuk dilakukan analisis.

supplies_collection = mongo(collection="sales",
                            db="sample_supplies",
                            url=connection_string)

Mengeluarkan jumlah document pada supplies_collection.

supplies_collection$count()
## [1] 5000

Struktur Data

supplies_collection$iterate()$one()
## $saleDate
## [1] "2015-03-24 04:06:49 +07"
## 
## $items
## $items[[1]]
## $items[[1]]$name
## [1] "printer paper"
## 
## $items[[1]]$tags
## $items[[1]]$tags[[1]]
## [1] "office"
## 
## $items[[1]]$tags[[2]]
## [1] "stationary"
## 
## 
## $items[[1]]$price
## [1] 40.01
## 
## $items[[1]]$quantity
## [1] 2
## 
## 
## $items[[2]]
## $items[[2]]$name
## [1] "notepad"
## 
## $items[[2]]$tags
## $items[[2]]$tags[[1]]
## [1] "office"
## 
## $items[[2]]$tags[[2]]
## [1] "writing"
## 
## $items[[2]]$tags[[3]]
## [1] "school"
## 
## 
## $items[[2]]$price
## [1] 35.29
## 
## $items[[2]]$quantity
## [1] 2
## 
## 
## $items[[3]]
## $items[[3]]$name
## [1] "pens"
## 
## $items[[3]]$tags
## $items[[3]]$tags[[1]]
## [1] "writing"
## 
## $items[[3]]$tags[[2]]
## [1] "office"
## 
## $items[[3]]$tags[[3]]
## [1] "school"
## 
## $items[[3]]$tags[[4]]
## [1] "stationary"
## 
## 
## $items[[3]]$price
## [1] 56.12
## 
## $items[[3]]$quantity
## [1] 5
## 
## 
## $items[[4]]
## $items[[4]]$name
## [1] "backpack"
## 
## $items[[4]]$tags
## $items[[4]]$tags[[1]]
## [1] "school"
## 
## $items[[4]]$tags[[2]]
## [1] "travel"
## 
## $items[[4]]$tags[[3]]
## [1] "kids"
## 
## 
## $items[[4]]$price
## [1] 77.71
## 
## $items[[4]]$quantity
## [1] 2
## 
## 
## $items[[5]]
## $items[[5]]$name
## [1] "notepad"
## 
## $items[[5]]$tags
## $items[[5]]$tags[[1]]
## [1] "office"
## 
## $items[[5]]$tags[[2]]
## [1] "writing"
## 
## $items[[5]]$tags[[3]]
## [1] "school"
## 
## 
## $items[[5]]$price
## [1] 18.47
## 
## $items[[5]]$quantity
## [1] 2
## 
## 
## $items[[6]]
## $items[[6]]$name
## [1] "envelopes"
## 
## $items[[6]]$tags
## $items[[6]]$tags[[1]]
## [1] "stationary"
## 
## $items[[6]]$tags[[2]]
## [1] "office"
## 
## $items[[6]]$tags[[3]]
## [1] "general"
## 
## 
## $items[[6]]$price
## [1] 19.95
## 
## $items[[6]]$quantity
## [1] 8
## 
## 
## $items[[7]]
## $items[[7]]$name
## [1] "envelopes"
## 
## $items[[7]]$tags
## $items[[7]]$tags[[1]]
## [1] "stationary"
## 
## $items[[7]]$tags[[2]]
## [1] "office"
## 
## $items[[7]]$tags[[3]]
## [1] "general"
## 
## 
## $items[[7]]$price
## [1] 8.08
## 
## $items[[7]]$quantity
## [1] 3
## 
## 
## $items[[8]]
## $items[[8]]$name
## [1] "binder"
## 
## $items[[8]]$tags
## $items[[8]]$tags[[1]]
## [1] "school"
## 
## $items[[8]]$tags[[2]]
## [1] "general"
## 
## $items[[8]]$tags[[3]]
## [1] "organization"
## 
## 
## $items[[8]]$price
## [1] 14.16
## 
## $items[[8]]$quantity
## [1] 3
## 
## 
## 
## $storeLocation
## [1] "Denver"
## 
## $customer
## $customer$gender
## [1] "M"
## 
## $customer$age
## [1] 42
## 
## $customer$email
## [1] "cauho@witwuta.sv"
## 
## $customer$satisfaction
## [1] 4
## 
## 
## $couponUsed
## [1] TRUE
## 
## $purchaseMethod
## [1] "Online"

Query Data

Query yang dilakukan adalah untuk mencari jumlah pembelian pada masing masing metode.

query_data = supplies_collection$aggregate('[{"$group":
                                                { "_id":"$purchaseMethod",
                                                  "Count": {"$sum":1}
                                           }}]')
_id Count
Phone 596
Online 1585
In store 2819

Visualisasi Data

Visualisasi data yang digunakan untuk menggambarkan hasil analisis data tersebut adalah berupa pie chart.

library(ggplot2)

ggplot(query_data, aes(x="", y=perc, fill = Metode)) +
  geom_bar(stat="identity", width=1, color="white") +
  scale_fill_manual(values = c("#6fafad","#6f8a91","#dbb376"))+
  geom_text(aes(label = labels),
            position = position_stack(vjust = 0.5)) +
  coord_polar("y", start=0)+
  theme_void()