Learn R Programming

mongolite (version 0.2)

mongo: MongoDB client

Description

Connect to a MongoDB collection.

Usage

mongo(collection = "test", db = "test", url = "mongodb://localhost")

Arguments

collection
name of collection
db
name of database
url
address of the mongodb server

Value

  • Upon success returns a pointer to a collection on the server. The collection can be interfaced using the methods described below.

emph

arXiv:1403.2805

url

http://arxiv.org/abs/1403.2805

References

Jeroen Ooms (2014). The jsonlite Package: A Practical and Consistent Mapping Between JSON Data and R

Examples

Run this code
# 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('{"$query":{"month":1,"day":1}, "$orderby":{"distance":-1}}')
head(jan1)

# Select columns
jan1 <- m$find('{"month":1,"day":1}', fields = '{"_id":0, "distance":1, "carrier":1}')

# Tabulate
m$aggregate('[{"$group":{"_id":"$carrier", "count": {"$sum":1}, "average":{"$avg":"$distance"}}}]')

Run the code above in your browser using DataLab