mongolite

Fast and Simple MongoDB Client for R

Build Status AppVeyor Build Status Coverage Status CRAN_Status_Badge CRAN RStudio mirror downloads Research software impact Github Stars

High-level, high-performance MongoDB client based on libmongoc and jsonlite. Includes support for aggregation, indexing, map-reduce, streaming, SSL encryption and SASL authentication. The vignette gives a brief overview of the available methods in the package.

Documentation

About the R package:

Hello World

Example using a public test server

# Connect to mongolabs
con <- mongo("mtcars", url = "mongodb://readwrite:test@ds043942.mongolab.com:43942/jeroen_test")

# Wipe collection
if(con$count() > 0) 
  con$drop()
  
# Insert some data
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()

Insert/retrieve data from your local mongodb server:

# Init connection to local mongod
library(mongolite)
m <- mongo(collection = "diamonds")

# Insert test data
data(diamonds, package="ggplot2")
m$insert(diamonds)

# Check records
m$count()
nrow(diamonds)

# Perform a query and retrieve data
out <- m$find('{"cut" : "Premium", "price" : { "$lt" : 1000 } }')

# Compare
nrow(out)
nrow(subset(diamonds, cut == "Premium" & price < 1000))

More advanced features include map reduce:

# Cross-table
tbl <- m$mapreduce(
  map = "function(){emit({cut:this.cut, color:this.color}, 1)}",
  reduce = "function(id, counts){return Array.sum(counts)}"
)
# Same as:
data.frame(with(diamonds, table(cut, color)))

Importing and exporting json or bson data:

# Stream jsonlines into a connection
tmp <- tempfile()
m$export(file(tmp))

# Stream it back in R
library(jsonlite)
mydata <- stream_in(file(tmp))

# Or into mongo
m2 <- mongo("diamonds2")
m2$count()
m2$import(file(tmp))
m2$count()

# Remove the collection
m$drop()
m2$drop()

Installation

Binary packages for OS-X or Windows can be installed directly from CRAN:

install.packages("mongolite")

Installation from source on Linux requires openssl and Cyrus SASL (not GNU sasl). On Debian or Ubuntu use libssl-dev and libsasl2-dev:

sudo apt-get install -y libssl-dev libsasl2-dev

On Fedora, CentOS or RHEL use openssl-devel and cyrus-sasl-devel:

sudo yum install openssl-devel cyrus-sasl-devel

On OS-X sasl is included with the system so only openssl is needed.

brew install openssl


mongolite

Fast and Simple MongoDB Client for R

建立状态 AppVeyor建立状态 覆盖状态 CRAN_Status_Badge 研究软件影响 Github的星

High-level, high-performance MongoDB client based on libmongoc and jsonlite. Includes support for aggregation, indexing, map-reduce, streaming, SSL encryption and SASL authentication. The vignette gives a brief overview of the available methods in the package.

文件

关于R包:

Hello World

使用公共测试服务器的示例

# Connect to mongolabs
con <- mongo("mtcars", url = "mongodb://readwrite:test@ds043942.mongolab.com:43942/jeroen_test")

# Wipe collection if(con$count() > 0) con$drop()

# Insert some data 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()

从您的本地mongodb服务器插入/检索数据:

# Init connection to local mongod
library(mongolite)
m <- mongo(collection = "diamonds")

# Insert test data data(diamonds, package="ggplot2") m$insert(diamonds)

# Check records m$count() nrow(diamonds)

# Perform a query and retrieve data out <- m$find('{"cut" : "Premium", "price" : { "$lt" : 1000 } }')

# Compare nrow(out) nrow(subset(diamonds, cut == "Premium" & price < 1000))

更高级的功能包括map reduce:

# Cross-table
tbl <- m$mapreduce(
  map = "function(){emit({cut:this.cut, color:this.color}, 1)}",
  reduce = "function(id, counts){return Array.sum(counts)}"
)
# Same as:
data.frame(with(diamonds, table(cut, color)))

导入和导出json或bson数据:

# Stream jsonlines into a connection
tmp <- tempfile()
m$export(file(tmp))

# Stream it back in R library(jsonlite) mydata <- stream_in(file(tmp))

# Or into mongo m2 <- mongo("diamonds2") m2$count() m2$import(file(tmp)) m2$count()

# Remove the collection m$drop() m2$drop()

安装

OS-X Windows 的二进制包可以直接从CRAN安装:

install.packages("mongolite")

从Linux上的源安装需要 openssl Cyrus SASL GNU sasl )。在 Debian Ubuntu 上使用 libssl-dev 和<一个href =https://packages.debian.org/testing/libsasl2-dev> libsasl2-dev :

sudo apt-get install -y libssl-dev libsasl2-dev

Fedora CentOS或RHEL 上使用 openssl-devel < / a>和 cyrus-sasl-devel

sudo yum install openssl-devel cyrus-sasl-devel

OS-X 系统中包含sasl,因此只有 openssl 是需要的。

brew install openssl




相关问题推荐