# Connect to demo server
con <- mongo("mtcars", url =
"mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test")
if(con$count() > 0) con$drop()
con$insert(mtcars)
stopifnot(con$count() == nrow(mtcars))
# Query data
mydata <- con$find()
stopifnot(all.equal(mydata, mtcars))
con$drop()
# Automatically disconnect when connection is removed
rm(con)
gc()
if (FALSE) {
# dplyr example
library(nycflights13)
# Insert some data
m <- mongo(collection = "nycflights")
m$drop()
m$insert(flights)
# Basic queries
m$count('{"month":1, "day":1}')
jan1 <- m$find('{"month":1, "day":1}')
# Sorting
jan1 <- m$find('{"month":1,"day":1}', sort='{"distance":-1}')
head(jan1)
# Sorting on large data requires index
m$index(add = "distance")
allflights <- m$find(sort='{"distance":-1}')
# Select columns
jan1 <- m$find('{"month":1,"day":1}', fields = '{"_id":0, "distance":1, "carrier":1}')
# List unique values
m$distinct("carrier")
m$distinct("carrier", '{"distance":{"$gt":3000}}')
# Tabulate
m$aggregate('[{"$group":{"_id":"$carrier", "count": {"$sum":1}, "average":{"$avg":"$distance"}}}]')
# Map-reduce (binning)
hist <- m$mapreduce(
map = "function(){emit(Math.floor(this.distance/100)*100, 1)}",
reduce = "function(id, counts){return Array.sum(counts)}"
)
# Stream jsonlines into a connection
tmp <- tempfile()
m$export(file(tmp))
# Remove the collection
m$drop()
# Import from jsonlines stream from connection
dmd <- mongo("diamonds")
dmd$import(url("http://jeroen.github.io/data/diamonds.json"))
dmd$count()
# Export
dmd$drop()
}
Run the code above in your browser using DataLab