Skip to content

Generates one-dimensional and two-dimensional frequency tables from data stored in a MongoDB database.

License

Notifications You must be signed in to change notification settings

ingmarboeschen/mongoTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoTable::mongoTable()

This function generates one-dimensional and two-dimensional frequency tables from data stored in a MongoDB database. Rather than retrieving the entire dataset through a find() operation, this function leverages MongoDB's aggregation capabilities to compile frequency data directly on the server, followed by post-processing in R.

Usage

mongoTable(connection, x, y = NULL, query = "{}",lowerize = FALSE, limit = NULL, sort = FALSE, decreasing = TRUE)

Arguments

Name Description
connection character. A mongo connection object initiated with mongolite::mongo().
x character. A field variable for which frequencies should be counted.
y character. An optional second field variable for which frequencies should be counted.
query character. An optional MongoDB query for data subset selection (e.g.: ’{"year": 2024}’).
lowerize logical. All levels in one dimensional tables will be lowerized.
limit integer. Defines the maximum length/dimensions of output.
sort logical. If TRUE, the output is sorted by frequency.
decreasing logical. If TRUE and sort==TRUE, the output is returned with decreasing frequencies. If TRUE and sort==FALSE, level names are returned in decreasing manner.

Installation with the devtools package

if(require(devtools)!=TRUE) install.packages("devtools")
devtools::install_github("ingmarboeschen/mongoTable")

Examples

## use mongolite::mongo() to connect to a MongoDB instance (demo server)
mon <- mongolite::mongo("mtcars", url = "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test")
if(mon$count() > 0) mon$drop()
mon$insert(mtcars)
stopifnot(mon$count() == nrow(mtcars))

## Create a one-dimensional frequency table
# for all x
mongoTable(connection = "mon", x = "cyl")

# for all x matching a query (cars with mpg greater than 20)
mongoTable(connection="mon", x="cyl", query = '{\"mpg\": {\"$gt": 20}}')

## Create a two-dimensional frequency table
# for all x and y
mongoTable(con = "mon", x = "cyl", y = "gear")

# for all x and y matching a query (cars with mpg greater than 20)
mongoTable(con="mon", x = "cyl", y = "gear", query = '{\"mpg\": {\"$gt": 20}}')

About

Generates one-dimensional and two-dimensional frequency tables from data stored in a MongoDB database.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages