├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── R ├── onLoad.R └── rmongo.R ├── README ├── inst └── java │ └── r-mongo-scala-1.0-SNAPSHOT.jar ├── java └── README ├── man ├── RMongo-class.Rd ├── RMongo-package.Rd ├── dbAggregate-methods.Rd ├── dbAuthenticate-methods.Rd ├── dbDisconnect-methods.Rd ├── dbGetDistinct-methods.Rd ├── dbGetQuery-methods.Rd ├── dbGetQueryForKeys-methods.Rd ├── dbInsertDocument-methods.Rd ├── dbRemoveQuery-methods.Rd ├── dbSetWriteConcern-methods.Rd ├── dbShowCollections-methods.Rd ├── mongoDbConnect.Rd ├── mongoDbReplicaSetConnect.Rd └── mongoDbReplicaSetConnectWithAuthentication.Rd ├── src └── r-mongo-scala │ ├── .gitignore │ ├── README.txt │ ├── pom.xml │ └── src │ ├── main │ └── scala │ │ └── rmongo │ │ └── RMongo.scala │ └── test │ └── scala │ └── rmongo │ └── MongoTest.scala └── tests ├── RMongo-Ex.R └── RMongo-Ex.Rout.save /.Rbuildignore: -------------------------------------------------------------------------------- 1 | src 2 | .git 3 | .idea 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | src/r-mongo-scala/target 4 | *.iws 5 | *.ipr 6 | *.iml 7 | *.class 8 | .idea 9 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: RMongo 2 | Type: Package 3 | Title: MongoDB Client for R 4 | Version: 0.1.0 5 | Date: 2015-03-24 6 | Author: Tommy Chheng 7 | Maintainer: Tommy Chheng 8 | Description: MongoDB Database interface for R. The interface is provided via Java calls to the mongo-java-driver. 9 | License: GPL-3 10 | LazyLoad: yes 11 | Depends: R(>= 2.14.1), rJava, methods 12 | Suggests: RUnit 13 | SystemRequirements: Java (>= 1.7), MongoDB (>= 1.6) 14 | URL: http://github.com/tc/RMongo 15 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | import('methods') 2 | import('rJava') 3 | export('mongoDbConnect', 'mongoDbReplicaSetConnect','mongoDbReplicaSetConnectWithCredentials') 4 | exportClass('RMongo') 5 | exportMethods('dbInsertDocument', 'dbAuthenticate', 'dbSetWriteConcern', 'dbShowCollections', 'dbGetQuery', 'dbGetQueryForKeys', 'dbDisconnect', 'dbRemoveQuery', 'dbGetDistinct', 'dbAggregate') 6 | -------------------------------------------------------------------------------- /R/onLoad.R: -------------------------------------------------------------------------------- 1 | .onLoad <- function(libname, pkgname) { 2 | .jpackage(pkgname, lib.loc = libname) 3 | } 4 | -------------------------------------------------------------------------------- /R/rmongo.R: -------------------------------------------------------------------------------- 1 | library('rJava') 2 | .jinit() 3 | .jaddClassPath("inst/java/r-mongo-scala-1.0-SNAPSHOT.jar") 4 | 5 | setClass("RMongo", representation(javaMongo = "jobjRef")) 6 | 7 | mongoDbConnect <- function(dbName, host="127.0.0.1", port=27017){ 8 | rmongo <- new("RMongo", javaMongo = .jnew("rmongo/RMongo", dbName, host, as.integer(port))) 9 | rmongo 10 | } 11 | 12 | mongoDbReplicaSetConnect <- function(dbName, hosts="127.0.0.1:27017"){ 13 | rmongo <- new("RMongo", javaMongo = .jnew("rmongo/RMongo", dbName, hosts, FALSE)) 14 | dbDisconnect(rmongo) 15 | rmongo <- new("RMongo", javaMongo = .jnew("rmongo/RMongo", dbName, hosts, TRUE)) 16 | rmongo 17 | } 18 | 19 | mongoDbReplicaSetConnectWithCredentials <- function(dbName, hosts="127.0.0.1:27017", username, pwd){ 20 | rmongo <- new("RMongo", javaMongo = .jnew("rmongo/RMongo", dbName, hosts, TRUE, username, pwd)) 21 | rmongo 22 | } 23 | 24 | setGeneric("dbAuthenticate", function(rmongo.object, username, password) standardGeneric("dbAuthenticate")) 25 | setMethod("dbAuthenticate", signature(rmongo.object="RMongo", username="character", password="character"), 26 | function(rmongo.object, username, password){ 27 | results <- .jcall(rmongo.object@javaMongo, "Z", "dbAuthenticate", username, password) 28 | results 29 | } 30 | ) 31 | 32 | setGeneric("dbSetWriteConcern", function(rmongo.object, w, wtimeout, fsync, j) standardGeneric("dbSetWriteConcern")) 33 | setMethod("dbSetWriteConcern", signature(rmongo.object="RMongo", w="numeric", wtimeout="numeric", fsync="logical", j="logical"), 34 | function(rmongo.object, w, wtimeout, fsync, j){ 35 | .jcall(rmongo.object@javaMongo, "V", "dbSetWriteConcern", as.integer(w), as.integer(wtimeout), fsync, j) 36 | } 37 | ) 38 | 39 | setGeneric("dbShowCollections", function(rmongo.object) standardGeneric("dbShowCollections")) 40 | setMethod("dbShowCollections", signature(rmongo.object="RMongo"), 41 | function(rmongo.object){ 42 | results <- .jcall(rmongo.object@javaMongo, "[S", "dbShowCollections") 43 | results 44 | } 45 | ) 46 | 47 | setGeneric("dbInsertDocument", function(rmongo.object, collection, doc) standardGeneric("dbInsertDocument")) 48 | setMethod("dbInsertDocument", signature(rmongo.object="RMongo", collection="character", doc="character"), 49 | function(rmongo.object, collection, doc){ 50 | results <- .jcall(rmongo.object@javaMongo, "S", "dbInsertDocument", collection, doc) 51 | results 52 | } 53 | ) 54 | 55 | setGeneric("dbGetQueryForKeys", function(rmongo.object, collection, query, keys, skip=0, limit=1000) standardGeneric("dbGetQueryForKeys")) 56 | setMethod("dbGetQueryForKeys", signature(rmongo.object="RMongo", collection="character", query="character", keys="character", skip='numeric', limit='numeric'), 57 | function(rmongo.object, collection, query, keys, skip, limit){ 58 | results <- .jcall(rmongo.object@javaMongo, "S", "dbGetQuery", collection, query, keys, skip, limit) 59 | if(results == ""){ 60 | data.frame() 61 | }else{ 62 | con <- textConnection(results) 63 | data.frame.results <- read.csv(con, sep="", stringsAsFactors=FALSE, quote="") 64 | close(con) 65 | 66 | data.frame.results 67 | } 68 | } 69 | ) 70 | 71 | setMethod("dbGetQueryForKeys", signature(rmongo.object="RMongo", collection="character", query="character", keys="character", skip='missing', limit='missing'), 72 | function(rmongo.object, collection, query, keys, skip, limit){ 73 | dbGetQueryForKeys(rmongo.object, collection, query, keys, 0, 1000) 74 | } 75 | ) 76 | 77 | setGeneric("dbGetQuery", function(rmongo.object, collection, query, skip=0, limit=1000) standardGeneric("dbGetQuery")) 78 | setMethod("dbGetQuery", signature(rmongo.object="RMongo", collection="character", query="character"), 79 | function(rmongo.object, collection, query, skip, limit){ 80 | dbGetQueryForKeys(rmongo.object, collection, query, "{}", skip, limit) 81 | } 82 | ) 83 | 84 | setGeneric("dbDisconnect", function(rmongo.object) standardGeneric("dbDisconnect")) 85 | setMethod("dbDisconnect", signature(rmongo.object="RMongo"), 86 | function(rmongo.object){ 87 | .jcall(rmongo.object@javaMongo, "V", "close") 88 | } 89 | ) 90 | 91 | setGeneric("dbRemoveQuery", function(rmongo.object, collection, query) standardGeneric("dbRemoveQuery")) 92 | setMethod("dbRemoveQuery", signature(rmongo.object="RMongo", collection="character", query="character"), 93 | function(rmongo.object, collection, query){ 94 | results <- .jcall(rmongo.object@javaMongo, "S", "dbRemoveQuery", collection, query) 95 | results 96 | } 97 | ) 98 | 99 | setGeneric("dbGetDistinct", function(rmongo.object, collection, key, query="") standardGeneric("dbGetDistinct")) 100 | setMethod("dbGetDistinct", signature(rmongo.object="RMongo", collection="character", key="character", query="missing"), 101 | function(rmongo.object, collection, key, query=""){ 102 | dbGetDistinct(rmongo.object, collection, key, "") 103 | } 104 | ) 105 | 106 | setMethod("dbGetDistinct", signature(rmongo.object="RMongo", collection="character", key="character", query="character"), 107 | function(rmongo.object, collection, key, query){ 108 | results <- .jcall(rmongo.object@javaMongo, "S", "dbGetDistinct", collection, key, query) 109 | if(results == ""){ 110 | vector(mode="character") 111 | }else{ 112 | con <- textConnection(results) 113 | data.frame.results <- read.table(con, sep="\n", stringsAsFactors=FALSE, quote="\"", header=FALSE) 114 | close(con) 115 | 116 | as.vector(t(data.frame.results)) 117 | } 118 | } 119 | ) 120 | 121 | setGeneric("dbAggregate", function(rmongo.object, collection, query="") standardGeneric("dbAggregate")) 122 | setMethod("dbAggregate", signature(rmongo.object="RMongo", collection="character", query="character"), 123 | function(rmongo.object, collection, query){ 124 | results <- .jcall(rmongo.object@javaMongo, "[S", "dbAggregate", collection, .jarray(query)) 125 | results 126 | } 127 | ) 128 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | RMongo 2 | http://github.com/tc/RMongo 3 | ---- 4 | @tommychheng 5 | 6 | This is a R package which lets you query MongoDB databases. 7 | 8 | The sources for the java files are in ``src/r-mongo-scala`` 9 | 10 | Feel free to report bugs/feature requests on github. 11 | 12 | Validate: 13 | cd .. 14 | R CMD check RMongo 15 | 16 | Build: 17 | R CMD build RMongo 18 | 19 | Install: 20 | R CMD install RMongo*.tar.gz 21 | 22 | Publish to CRAN: 23 | Visit http://cran.r-project.org/ and use the submission form. 24 | 25 | CHANGELOG 26 | --- 27 | 0.1.0 28 | Updated scala to 2.11.1 and mongo-java-driver to 2.13.0 29 | 30 | 0.0.25 31 | Restored R 2.14 comp. Added replicaset support. thanks @benoitlouy 32 | 33 | 0.0.24 34 | Recompiled to support >R 3. Updated mongo-java-driver to 2.11.2 35 | 36 | 0.0.23 37 | Added aggregation framework support. thanks @jayjacobs 38 | 39 | 0.0.22 40 | Fixed a bug in toCsvOutput which caused R data frames to be restricted to the fields of the first record found by a query. Thanks @tunnuz 41 | Added dbRemoveQuery, dbGetDistinct methods. Thanks @liuq 42 | 43 | --- 44 | # --------------------------------------------------------------------------- 45 | # Copyright 2010, Tommy Chheng. 46 | # 47 | # RMongo is free software: you can redistribute it and/or modify 48 | # it under the terms of the GNU General Public License as published by 49 | # the Free Software Foundation, either version 3 of the License, or 50 | # (at your option) any later version. 51 | # 52 | # RMongo is distributed in the hope that it will be useful, 53 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 54 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 55 | # GNU General Public License for more details. 56 | # 57 | # You should have received a copy of the GNU General Public License 58 | # along with RMongo. If not, see 49 | http://tommy.chheng.com 50 | } 51 | \keyword{mongodb} 52 | \keyword{mongo} 53 | -------------------------------------------------------------------------------- /man/dbAggregate-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbAggregate-methods} 2 | \docType{methods} 3 | \alias{dbAggregate} 4 | \alias{dbAggregate-methods} 5 | \alias{dbAggregate,RMongo,character,character-method} 6 | 7 | \title{Performing a MongoDB aggregate query} 8 | 9 | \description{ 10 | Send a set of aggregate pipeline commands to mongodb. 11 | The output is a data.frame object and will work properly only if the mongoDB collection contains primitive data types. It may not work properly if there are any embedded documents or arrays. 12 | } 13 | 14 | \usage{ 15 | dbAggregate(rmongo.object, collection, query) 16 | } 17 | 18 | \arguments{ 19 | \item{rmongo.object}{The RMongo object.} 20 | \item{collection}{The name of the collection the query is being performed upon.} 21 | \item{query}{A vector of pipeline command in JSON format. See http://docs.mongodb.org/manual/reference/aggregation/ for more information on the MongoDB aggregation framework.} 22 | } 23 | 24 | \seealso{ 25 | \code{\link{mongoDbConnect}} 26 | } 27 | \examples{ 28 | mongo <- mongoDbConnect("test") 29 | # insert two records 30 | dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5 }') 31 | dbInsertDocument(mongo, "test_data", '{"foo": "nl", "size": 10 }') 32 | output <- dbAggregate(mongo, "test_data", c(' { "$project" : { "baz" : "$foo" } } ', 33 | ' { "$group" : { "_id" : "$baz" } } ', 34 | ' { "$match" : { "_id" : "bar" } } ')) 35 | print(output) 36 | } 37 | -------------------------------------------------------------------------------- /man/dbAuthenticate-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbAuthenticate-methods} 2 | \docType{methods} 3 | \alias{dbAuthenticate} 4 | \alias{dbAuthenticate-methods} 5 | \alias{dbAuthenticate,RMongo,character,character-method} 6 | 7 | \title{Authenticate with a username/password.} 8 | 9 | \description{ 10 | This is an optional authenticate method. 11 | } 12 | 13 | \usage{ 14 | dbAuthenticate(rmongo.object, username, password) 15 | } 16 | 17 | \arguments{ 18 | \item{rmongo.object}{RMongo object containing the database connection information.} 19 | \item{username}{Username} 20 | \item{password}{Password} 21 | } 22 | 23 | \seealso{ 24 | \code{\link{dbAuthenticate}} 25 | } 26 | \examples{ 27 | mongo <- mongoDbConnect("test") 28 | username = "" 29 | password = "" 30 | authenticated <- dbAuthenticate(mongo, username, password) 31 | dbDisconnect(mongo) 32 | } 33 | 34 | -------------------------------------------------------------------------------- /man/dbDisconnect-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbDisconnect-methods} 2 | \docType{methods} 3 | \alias{dbDisconnect} 4 | \alias{dbDisconnect-methods} 5 | \alias{dbDisconnect,RMongo-method} 6 | 7 | \title{Disconnect from the MongoDB database} 8 | 9 | \description{ 10 | This will close the connect from the MongoDB database. 11 | } 12 | 13 | \usage{ 14 | dbDisconnect(rmongo.object) 15 | } 16 | 17 | \arguments{ 18 | \item{rmongo.object}{RMongo object containing the database connection information.} 19 | } 20 | 21 | \seealso{ 22 | \code{\link{mongoDbConnect}} 23 | } 24 | \examples{ 25 | mongo <- mongoDbConnect("test") 26 | dbDisconnect(mongo) 27 | } 28 | -------------------------------------------------------------------------------- /man/dbGetDistinct-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbGetDistinct-methods} 2 | \docType{methods} 3 | \alias{dbGetDistinct} 4 | \alias{dbGetDistinct-methods} 5 | \alias{dbGetDistinct,RMongo,character,character,missing-method} 6 | \alias{dbGetDistinct,RMongo,character,character,character-method} 7 | 8 | \title{Performing a distinct MongoDB query} 9 | 10 | \description{ 11 | Send a json query to mongodb. 12 | The output is a data.frame object and will work properly only if the mongoDB collection contains primitive data types. It may not work properly if there are any embedded documents or arrays. 13 | } 14 | 15 | \usage{ 16 | dbGetDistinct(rmongo.object, collection, key, query) 17 | } 18 | 19 | \arguments{ 20 | \item{rmongo.object}{The RMongo object.} 21 | \item{collection}{The name of the collection the query is being performed upon.} 22 | \item{key}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.} 23 | \item{query}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.} 24 | } 25 | 26 | \seealso{ 27 | \code{\link{mongoDbConnect}} 28 | } 29 | \examples{ 30 | mongo <- mongoDbConnect("test") 31 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 32 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 33 | output <- dbGetDistinct(mongo, 'test_data', '{"foo": "bar"}') 34 | print(output) 35 | } 36 | 37 | -------------------------------------------------------------------------------- /man/dbGetQuery-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbGetQuery-methods} 2 | \docType{methods} 3 | \alias{dbGetQuery} 4 | \alias{dbGetQuery-methods} 5 | \alias{dbGetQuery,RMongo,character,character-method} 6 | \alias{dbGetQuery,RMongo,character,character,missing,missing-method} 7 | \alias{dbGetQuery,RMongo,character,character,numeric,numeric-method} 8 | 9 | \title{Performing a MongoDB query} 10 | 11 | \description{ 12 | Send a json query to mongodb. 13 | The output is a data.frame object and will work properly only if the mongoDB collection contains primitive data types. It may not work properly if there are any embedded documents or arrays. 14 | } 15 | 16 | \usage{ 17 | dbGetQuery(rmongo.object, collection, query, skip=0, limit=1000) 18 | } 19 | 20 | \arguments{ 21 | \item{rmongo.object}{The RMongo object.} 22 | \item{collection}{The name of the collection the query is being performed upon.} 23 | \item{query}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.} 24 | \item{skip}{Offset the resultset. Can be used with limit to perform pagination.} 25 | \item{limit}{Limits the resultset size.} 26 | } 27 | 28 | \seealso{ 29 | \code{\link{mongoDbConnect}} 30 | } 31 | \examples{ 32 | mongo <- mongoDbConnect("test") 33 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 34 | output <- dbGetQuery(mongo, 'test_data', '{"foo": "bar"}') 35 | print(output) 36 | 37 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar with spaces"}') 38 | output <- dbGetQuery(mongo, 'test_data', '{"foo": { "$regex": "bar*", "$options": "i"} }') 39 | } 40 | -------------------------------------------------------------------------------- /man/dbGetQueryForKeys-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbGetQueryForKeys-methods} 2 | \docType{methods} 3 | \alias{dbGetQueryForKeys} 4 | \alias{dbGetQueryForKeys-methods} 5 | \alias{dbGetQueryForKeys,RMongo,character,character,character-method} 6 | \alias{dbGetQueryForKeys,RMongo,character,character,character,missing,missing-method} 7 | \alias{dbGetQueryForKeys,RMongo,character,character,character,numeric,numeric-method} 8 | 9 | \title{Performing a MongoDB query and only return back certain keys} 10 | 11 | \description{ 12 | Send a json query to mongodb. 13 | The output is a data.frame object and will work properly only if the mongoDB collection contains primitive data types. It may not work properly if there are any embedded documents or arrays. 14 | } 15 | 16 | \usage{ 17 | dbGetQueryForKeys(rmongo.object, collection, query, keys, skip=0, limit=1000) 18 | } 19 | 20 | \arguments{ 21 | \item{rmongo.object}{The RMongo object.} 22 | \item{collection}{The name of the collection the query is being performed upon.} 23 | \item{query}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.} 24 | \item{keys}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.} 25 | \item{skip}{Offset the resultset. Can be used with limit to perform pagination.} 26 | \item{limit}{Limits the resultset size.} 27 | } 28 | 29 | \seealso{ 30 | \code{\link{mongoDbConnect}} 31 | } 32 | \examples{ 33 | mongo <- mongoDbConnect("test") 34 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 35 | output <- dbGetQueryForKeys(mongo, 'test_data', '{"foo": "bar"}', '{"foo":1}') 36 | print(output) 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /man/dbInsertDocument-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbInsertDocument-methods} 2 | \docType{methods} 3 | \alias{dbInsertDocument} 4 | \alias{dbInsertDocument-methods} 5 | \alias{dbInsertDocument,RMongo,character,character-method} 6 | 7 | \title{Insert a document into a MongoDB collection} 8 | 9 | \description{ 10 | Insert a json character object into the mongodb collection. 11 | } 12 | 13 | \usage{ 14 | dbInsertDocument(rmongo.object, collection, doc) 15 | } 16 | 17 | \arguments{ 18 | \item{rmongo.object}{The RMongo object.} 19 | \item{collection}{The name of the collection the document is being inserted into.} 20 | \item{doc}{A JSON string document. } 21 | } 22 | 23 | \seealso{ 24 | \code{\link{dbGetQuery-methods}} 25 | } 26 | \examples{ 27 | mongo <- mongoDbConnect("test") 28 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 29 | dbDisconnect(mongo) 30 | } 31 | -------------------------------------------------------------------------------- /man/dbRemoveQuery-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbRemoveQuery-methods} 2 | \docType{methods} 3 | \alias{dbRemoveQuery} 4 | \alias{dbRemoveQuery-methods} 5 | \alias{dbRemoveQuery,RMongo,character,character-method} 6 | 7 | \title{Performing a delete MongoDB query} 8 | 9 | \description{ 10 | Send a json query to mongodb. 11 | } 12 | 13 | \usage{ 14 | dbRemoveQuery(rmongo.object, collection, query) 15 | } 16 | 17 | \arguments{ 18 | \item{rmongo.object}{The RMongo object.} 19 | \item{collection}{The name of the collection the query is being performed upon.} 20 | \item{query}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.} 21 | } 22 | 23 | \seealso{ 24 | \code{\link{mongoDbConnect}} 25 | } 26 | \examples{ 27 | mongo <- mongoDbConnect("test") 28 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 29 | output <- dbRemoveQuery(mongo, 'test_data', '{"foo": "bar"}') 30 | print(output) 31 | } 32 | 33 | -------------------------------------------------------------------------------- /man/dbSetWriteConcern-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbSetWriteConcern-methods} 2 | \docType{methods} 3 | \alias{dbSetWriteConcern} 4 | \alias{dbSetWriteConcern-methods} 5 | \alias{dbSetWriteConcern,RMongo,numeric,numeric,logical,logical-method} 6 | 7 | \title{Set write concern.} 8 | 9 | \description{ 10 | This is an optional method to set the write concern for a given Mongo DB connection. By default the Mongo DB library does not check if a write (insert, update, remove) actually succeed. 11 | } 12 | 13 | \usage{ 14 | dbSetWriteConcern(rmongo.object, w, wtimeout, fsync, j) 15 | } 16 | 17 | \arguments{ 18 | \item{rmongo.object}{RMongo object containing the database connection information.} 19 | \item{w}{Number of write to aknowledge. -1 Don't even report network errors. 0 Don't wait for acknowledgement from the server. 1 Wait for acknowledgement but don't wait for secondaries to replicate. 2+ Wait for one or more secondaries to also acknowledge.} 20 | \item{wtimeout}{How long to wait for slaves before failing. 0 indefinite. Greater than 0 milliseconds to wait.} 21 | \item{j}{Wait for group commit to journal.} 22 | \item{fsync}{Force fsync to disk.} 23 | } 24 | 25 | \examples{ 26 | mongo <- mongoDbConnect("test") 27 | dbSetWriteConcern(mongo, 1, 0, FALSE, FALSE) 28 | dbDisconnect(mongo) 29 | } 30 | -------------------------------------------------------------------------------- /man/dbShowCollections-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{dbShowCollections-methods} 2 | \docType{methods} 3 | \alias{dbShowCollections} 4 | \alias{dbShowCollections-methods} 5 | \alias{dbShowCollections,RMongo-method} 6 | 7 | \title{Shows a list of collections} 8 | 9 | \description{ 10 | Shows a list of collections in the specified rmongo.object database. 11 | } 12 | 13 | \usage{ 14 | dbShowCollections(rmongo.object) 15 | } 16 | 17 | \arguments{ 18 | \item{rmongo.object}{The RMongo object.} 19 | } 20 | 21 | \seealso{ 22 | \code{\link{dbShowCollections-methods}} 23 | } 24 | \examples{ 25 | mongo <- mongoDbConnect("test") 26 | dbShowCollections(mongo) 27 | dbDisconnect(mongo) 28 | } 29 | 30 | -------------------------------------------------------------------------------- /man/mongoDbConnect.Rd: -------------------------------------------------------------------------------- 1 | \name{mongoDbConnect} 2 | \alias{mongoDbConnect} 3 | 4 | \title{Connecting to a MongoDB database} 5 | 6 | \description{ 7 | Creates a RMongo object to use with querying and inserting methods. 8 | } 9 | 10 | \usage{ 11 | mongoDbConnect(dbName, host="127.0.0.1", port=27017) 12 | } 13 | 14 | \arguments{ 15 | \item{dbName}{Database name.} 16 | \item{host}{ Host name. Default to localhost: 127.0.0.1. You can specify a remote host. {optional}} 17 | \item{port}{ Port number. Default to mongodb's default port 27017. {optional}} 18 | } 19 | 20 | \examples{ 21 | mongo <- mongoDbConnect("test", "127.0.0.1", 27017) 22 | } 23 | -------------------------------------------------------------------------------- /man/mongoDbReplicaSetConnect.Rd: -------------------------------------------------------------------------------- 1 | \name{mongoDbReplicaSetConnect} 2 | \alias{mongoDbReplicaSetConnect} 3 | 4 | \title{Connecting to a MongoDB Replica Set} 5 | 6 | \description{ 7 | Connects to a Mongo DB Replica Set and creates a RMongo object to use with querying and inserting methods. 8 | } 9 | 10 | \usage{ 11 | mongoDbReplicaSetConnect(dbName, hosts="127.0.0.1:27017") 12 | } 13 | 14 | \arguments{ 15 | \item{dbName}{Database name.} 16 | \item{hosts}{ Comma separated list of hosts member of the replica set. Each entry in the list must have the format [:] . If is omitted, 27017 is used {optional}} 17 | } 18 | 19 | \examples{ 20 | mongo <- mongoDbReplicaSetConnect("test", "localhost:27017") 21 | } 22 | -------------------------------------------------------------------------------- /man/mongoDbReplicaSetConnectWithAuthentication.Rd: -------------------------------------------------------------------------------- 1 | \name{mongoDbReplicaSetConnectWithAuthentication} 2 | \alias{mongoDbReplicaSetConnectWithAuthentication} 3 | 4 | \title{Connecting to a MongoDB Replica Set with new default Authentication SCARM-SHA1} 5 | 6 | \description{ 7 | Connects to a Mongo DB Replica Set and creates a RMongo object to use with querying and inserting methods. 8 | } 9 | 10 | \usage{ 11 | mongoDbReplicaSetConnect(dbName, hosts="127.0.0.1:27017",username, password) 12 | } 13 | 14 | \arguments{ 15 | \item{dbName}{Database name.} 16 | \item{hosts}{ Comma separated list of hosts member of the replica set. Each entry in the list must have the format [:] . If is omitted, 27017 is used {optional}} 17 | \item{username}{Username as string.} 18 | \item{password}{Password as string.} 19 | } 20 | 21 | \examples{ 22 | mongo <- mongoDbReplicaSetConnectWithAuthentication("test", "localhost:27017", "alice", "secret") 23 | } 24 | -------------------------------------------------------------------------------- /src/r-mongo-scala/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | src/test/output 3 | *.iml 4 | *.ipr 5 | *.iws 6 | .*~ 7 | dist 8 | build 9 | target 10 | *.class 11 | 12 | -------------------------------------------------------------------------------- /src/r-mongo-scala/README.txt: -------------------------------------------------------------------------------- 1 | r-mongo-scala 2 | ---- 3 | For use with the RMongo r package. 4 | run mvn install to build jar to RMongo inst directory. 5 | and then continue with the instructions in the root README file 6 | 7 | # --------------------------------------------------------------------------- 8 | # Copyright 2010, Tommy Chheng. 9 | # 10 | # r-mongo-scala is free software: you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation, either version 3 of the License, or 13 | # (at your option) any later version. 14 | # 15 | # r-mongo-scala is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | # 20 | # You should have received a copy of the GNU General Public License 21 | # along with r-mongo-scala. If not, see 3 | 4.0.0 4 | rmongo 5 | r-mongo-scala 6 | jar 7 | 1.0-SNAPSHOT 8 | r-mongo-scala 9 | 10 | 11 | 2.11.1 12 | UTF-8 13 | 14 | 15 | 16 | 17 | maven.org 18 | http://repo2.maven.org/maven2/ 19 | 20 | 21 | 22 | scala-tools.org 23 | Scala-Tools Maven2 Repository 24 | http://scala-tools.org/repo-releases/ 25 | 26 | 27 | 28 | jboss 29 | http://repository.jboss.com/maven2 30 | 31 | true 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.scala-lang 39 | scala-library 40 | ${scala.version} 41 | 42 | 43 | 44 | junit 45 | junit 46 | 4.8.2 47 | test 48 | 49 | 50 | 51 | org.mongodb 52 | mongo-java-driver 53 | 2.13.0 54 | 55 | 56 | 57 | 58 | src/main/scala 59 | src/test/scala 60 | 61 | 62 | org.scala-tools 63 | maven-scala-plugin 64 | 2.15.2 65 | 66 | 67 | 68 | compile 69 | testCompile 70 | 71 | 72 | 73 | 74 | ${scala.version} 75 | 76 | -target:jvm-1.7 77 | 78 | 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-compiler-plugin 83 | 2.3.1 84 | 85 | 86 | UTF-8 87 | 1.6 88 | 1.6 89 | true 90 | 91 | 92 | 93 | 94 | maven-assembly-plugin 95 | 2.2-beta-5 96 | 97 | 98 | jar-with-dependencies 99 | 100 | 101 | 102 | 103 | 104 | org.apache.maven.plugins 105 | maven-shade-plugin 106 | 1.5 107 | 108 | 109 | package 110 | 111 | shade 112 | 113 | 114 | true 115 | 116 | 117 | 118 | 119 | 120 | 121 | maven-antrun-plugin 122 | 1.5 123 | 124 | 125 | install 126 | 127 | run 128 | 129 | 130 | 131 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /src/r-mongo-scala/src/main/scala/rmongo/RMongo.scala: -------------------------------------------------------------------------------- 1 | package rmongo 2 | 3 | import com.mongodb.util.JSON 4 | import collection.mutable.ListBuffer 5 | import collection.JavaConversions._ 6 | import com.mongodb._ 7 | import java.util.HashSet 8 | import java.util.Iterator 9 | import java.util.Arrays 10 | import java.util.logging.Logger 11 | import java.util.logging.Level 12 | 13 | class RMongo(dbName: String, hosts: String, replica: Boolean, username: String, pwd: String) { 14 | val mongoLogger = Logger.getLogger("com.mongodb") 15 | mongoLogger.setLevel(Level.SEVERE) 16 | val servers = hosts.split(",").map(_.trim.split(":")).map { a => 17 | if (a.size < 2) new ServerAddress(a(0), 27017) else new ServerAddress(a(0), a(1).toInt) 18 | }.toList 19 | val this.replica = replica 20 | val credential = mCreateCredential(username,dbName,pwd) 21 | val m = if (!this.replica) new MongoClient(servers(0)) else new MongoClient(servers,List(credential)) 22 | val db = m.getDB(dbName) 23 | var writeConcern = WriteConcern.NORMAL 24 | 25 | def this(dbName: String, host: String, port: Int) = this (dbName, host + ":" + port, false,"","") 26 | 27 | def this(dbName: String) = this (dbName, "127.0.0.1:27017", false,"","") 28 | 29 | def dbAuthenticate(username:String, password:String):Boolean = { 30 | db.authenticate(username, password.toCharArray) 31 | } 32 | 33 | def mCreateCredential(userName:String, database:String,password:String):MongoCredential = { 34 | MongoCredential.createCredential(username,database,password.toCharArray) 35 | } 36 | 37 | def dbSetWriteConcern(w: Int, wtimeout: Int, fsync: Boolean, j: Boolean) { 38 | writeConcern = new WriteConcern(w, wtimeout, fsync, j) 39 | } 40 | 41 | def dbShowCollections():Array[String] = { 42 | val names = db.getCollectionNames().map(name => name) 43 | 44 | names.toArray 45 | } 46 | 47 | def dbInsertDocument(collectionName: String, jsonDoc: String): String = { 48 | dbModificationAction(collectionName, jsonDoc, _.insert(_, writeConcern)) 49 | } 50 | 51 | def dbRemoveQuery(collectionName: String, query: String): String = { 52 | dbModificationAction(collectionName, query, _.remove(_, writeConcern)) 53 | } 54 | 55 | def dbModificationAction(collectionName: String, 56 | query: String, 57 | action:(DBCollection, DBObject) => WriteResult): String = { 58 | val dbCollection = db.getCollection(collectionName) 59 | 60 | val queryObject = JSON.parse(query).asInstanceOf[DBObject] 61 | val results = action(dbCollection, queryObject) 62 | 63 | if(results.getError == null) "ok" else results.getError 64 | } 65 | 66 | def dbGetQuery(collectionName: String, query: String): String = { 67 | dbGetQuery(collectionName, query, 0, 1000) 68 | } 69 | 70 | def dbGetQuery(collectionName: String, query: String, 71 | skipNum:Double, limitNum:Double):String = { 72 | dbGetQuery(collectionName, query, skipNum.toInt, limitNum.toInt) 73 | } 74 | 75 | def dbGetQuery(collectionName: String, query: String, 76 | skipNum:Int, limitNum:Int): String = { 77 | dbGetQuery(collectionName, query, "{}", skipNum, limitNum) 78 | } 79 | 80 | def dbGetQuery(collectionName:String, query:String, keys:String): String = { 81 | dbGetQuery(collectionName, query, keys, 0, 1000) 82 | } 83 | 84 | def dbGetQuery(collectionName: String, query: String, keys:String, 85 | skipNum:Double, limitNum:Double):String = { 86 | dbGetQuery(collectionName, query, keys, skipNum.toInt, limitNum.toInt) 87 | } 88 | 89 | def dbGetQuery(collectionName: String, query: String, keys: String, 90 | skipNum:Int, limitNum:Int): String = { 91 | val dbCollection = db.getCollection(collectionName) 92 | 93 | val queryObject = JSON.parse(query).asInstanceOf[DBObject] 94 | val keysObject = JSON.parse(keys).asInstanceOf[DBObject] 95 | val cursor = dbCollection.find(queryObject, keysObject).skip(skipNum).limit(limitNum) 96 | 97 | val results = RMongo.toCsvOutput(cursor) 98 | 99 | results 100 | } 101 | 102 | def dbGetDistinct(collectionName: String, key: String): String = { 103 | dbGetDistinct(collectionName, key, "") 104 | } 105 | 106 | def dbGetDistinct(collectionName: String, key: String, query: String): String = { 107 | val dbCollection = db.getCollection(collectionName) 108 | 109 | val queryObject = JSON.parse(query).asInstanceOf[DBObject] 110 | val distinctResults = dbCollection.distinct(key, queryObject).iterator 111 | val results = ListBuffer[String]() 112 | 113 | while (distinctResults.hasNext) { 114 | val item = distinctResults.next 115 | results.append("\"" + item.toString.replaceAll("\n", "\\n") + "\"") 116 | } 117 | 118 | results.mkString("\n") 119 | } 120 | 121 | //def dbAggregate(collectionName: String, queries: Array[String]): String = { 122 | def dbAggregate(collectionName: String, queries: Array[String]):Array[String] = { 123 | val dbCollection = db.getCollection(collectionName) 124 | val queryArray = new Array[DBObject](queries.length) 125 | for ( i <- 0 to (queries.length - 1) ) { 126 | val query = queries(i) 127 | queryArray(i) = JSON.parse(query).asInstanceOf[DBObject] 128 | } 129 | var aggregateIterator = dbCollection.aggregate(queryArray(0), Arrays.copyOfRange(queryArray, 1, queryArray.length):_*).results.iterator 130 | 131 | val results = ListBuffer[String]() 132 | while (aggregateIterator.hasNext) { 133 | val item = aggregateIterator.next 134 | // results.append("\"" + item.toString.replaceAll("\n", "\\n") + "\"") 135 | results.append(item.toString) 136 | } 137 | //results.mkString("\n") 138 | results.toArray 139 | } 140 | 141 | def close() { 142 | m.close() 143 | } 144 | 145 | def main() { 146 | 147 | } 148 | } 149 | 150 | object RMongo{ 151 | val SEPARATOR = "" 152 | 153 | def toJsonOutput(cursor:DBCursor): String = { 154 | val results = ListBuffer[String]() 155 | while (cursor.hasNext) { 156 | val item = cursor.next 157 | results.append(item.toString) 158 | } 159 | 160 | results.mkString("[", SEPARATOR, "]") 161 | } 162 | 163 | def toCsvOutput(cursor: DBCursor): String = { 164 | 165 | if(cursor.hasNext == false) return "" 166 | 167 | val results = ListBuffer[String]() 168 | 169 | if (cursor.getKeysWanted != null && cursor.getKeysWanted.keySet.size != 0) { 170 | /* If fields were specified (save time) */ 171 | val keysWanted = cursor.getKeysWanted 172 | keysWanted.put("_id", 1) 173 | val keys = keysWanted.keySet.toArray(new Array[String](keysWanted.keySet.size)) 174 | results.append(keys.mkString(SEPARATOR)) 175 | while (cursor.hasNext) { 176 | results.append(csvRowFromDBObject(keys, cursor.next)) 177 | } 178 | } else { 179 | /* Try to find set of desired keys by scanning cursor */ 180 | val keysWanted = new HashSet[String]() 181 | val ccursor:DBCursor = cursor.copy 182 | while(ccursor.hasNext == true) { 183 | keysWanted.addAll(ccursor.next.keySet) 184 | } 185 | val keys = keysWanted.toArray(new Array[String](keysWanted.size)) 186 | results.append(keys.mkString(SEPARATOR)) 187 | while (cursor.hasNext) { 188 | results.append(csvRowFromDBObject(keys, cursor.next)) 189 | } 190 | } 191 | 192 | results.mkString("\n") 193 | } 194 | 195 | def csvRowFromDBObject(keys:Array[String], item:DBObject):String = { 196 | 197 | keys.map{k => 198 | val value = item.get(k) 199 | 200 | if(value != null) 201 | value.toString.replaceAll("\"", "\\\"") 202 | else 203 | "" }.mkString(SEPARATOR) 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /src/r-mongo-scala/src/test/scala/rmongo/MongoTest.scala: -------------------------------------------------------------------------------- 1 | package rmongo 2 | 3 | import com.mongodb.util.JSON 4 | import org.junit.{Before, Assert, Test} 5 | import com.mongodb.{BasicDBList, Mongo, DB, DBObject} 6 | 7 | /** 8 | * 9 | * User: @tommychheng 10 | * Date: Sep 24, 2010 11 | * Time: 9:36:41 AM 12 | * 13 | * 14 | */ 15 | class MongoTest{ 16 | val m = new Mongo() 17 | val db = m.getDB("test") 18 | val collection = db.getCollection("test_data") 19 | 20 | def clearTestDB{ 21 | collection.drop() //make sure the collection is empty 22 | } 23 | 24 | @Before 25 | def insertDocs{ 26 | clearTestDB 27 | 28 | List(""" {"foo": "bar", "size": 5} """, 29 | """ {"foo": "n1", "size": 10} """).foreach{ doc => 30 | val docObject = JSON.parse(doc).asInstanceOf[DBObject] 31 | collection.insert(docObject) 32 | } 33 | } 34 | 35 | @Test 36 | def testDbShowCollections{ 37 | val rMongo = new RMongo("test") 38 | 39 | val collectionName = "test_data" 40 | 41 | assert(rMongo.dbShowCollections().contains(collectionName)) 42 | } 43 | 44 | @Test 45 | def testDbReplicaSetInsertDocument{ 46 | clearTestDB 47 | 48 | val rMongo = new RMongo("test", "localhost", true) 49 | val doc = """ {"_id": "foo", "foo": "bar", "size": 5} """ 50 | 51 | val response = rMongo.dbInsertDocument("test_data", doc) 52 | Assert.assertEquals("ok", response) 53 | 54 | val duplicateResponse = rMongo.dbInsertDocument("test_data", doc) 55 | var errMsg = """E11000 duplicate key error index: test.test_data.$_id_ dup key: { : "foo" }""" 56 | Assert.assertEquals(errMsg, duplicateResponse) 57 | } 58 | 59 | @Test 60 | def testDbInsertDocument{ 61 | clearTestDB 62 | 63 | val rMongo = new RMongo("test") 64 | val doc = """ {"_id": "foo", "foo": "bar", "size": 5} """ 65 | 66 | val response = rMongo.dbInsertDocument("test_data", doc) 67 | Assert.assertEquals("ok", response) 68 | 69 | val duplicateResponse = rMongo.dbInsertDocument("test_data", doc) 70 | var errMsg = """E11000 duplicate key error index: test.test_data.$_id_ dup key: { : "foo" }""" 71 | Assert.assertEquals(errMsg, duplicateResponse) 72 | } 73 | 74 | @Test 75 | def testDbRemoveQuery{ 76 | clearTestDB 77 | 78 | val rMongo = new RMongo("test") 79 | val doc = """ {"_id": "foo", "foo": "bar", "size": 5} """ 80 | 81 | val response = rMongo.dbInsertDocument("test_data", doc) 82 | Assert.assertEquals("ok", response) 83 | 84 | val removeResponse = rMongo.dbRemoveQuery("test_data", """ {"_id": "foo"} """) 85 | 86 | Assert.assertEquals("ok", removeResponse) 87 | } 88 | 89 | @Test 90 | def testDbGetQuery{ 91 | val rMongo = new RMongo("test") 92 | val results = rMongo.dbGetQuery("test_data", """ {} """) 93 | val record = parsedFirstRecordFrom(results) 94 | 95 | Assert.assertEquals("bar", record.getOrElse("foo", "")) 96 | } 97 | 98 | @Test 99 | def testDbGetQueryInner{ 100 | List(""" {"foo": "n1", "inner-parent": {"inner": 5} } """).foreach{ doc => 101 | val docObject = JSON.parse(doc).asInstanceOf[DBObject] 102 | collection.insert(docObject) 103 | } 104 | 105 | val rMongo = new RMongo("test") 106 | val results = rMongo.dbGetQuery("test_data", """ {"inner-parent": {"inner": 5}} """) 107 | val record = parsedFirstRecordFrom(results) 108 | 109 | Assert.assertEquals("""{ "inner" : 5}""", record.getOrElse("inner-parent", "")) 110 | } 111 | 112 | @Test 113 | def testDbGetQueryWithKeys{ 114 | val rMongo = new RMongo("test") 115 | val results = rMongo.dbGetQuery("test_data", """ {} """, """ {"foo": 1} """, 0, 100) 116 | val record = parsedFirstRecordFrom(results) 117 | 118 | Assert.assertEquals("bar", record.getOrElse("foo", "")) 119 | Assert.assertEquals("", record.getOrElse("size", "")) 120 | } 121 | 122 | @Test 123 | def testDbGetQueryRegex{ 124 | val rMongo = new RMongo("test") 125 | val results = rMongo.dbGetQuery("test_data", """ {"foo": {"$regex": "bar", "$options": "i"}} """) 126 | val record = parsedFirstRecordFrom(results) 127 | 128 | Assert.assertEquals("bar", record.getOrElse("foo", "")) 129 | } 130 | 131 | @Test 132 | def testDbGetQueryWithEmptyCollection{ 133 | val rMongo = new RMongo("test") 134 | val results = rMongo.dbGetQuery("empty_collection", "{}") 135 | 136 | Assert.assertEquals("", results) 137 | } 138 | 139 | @Test 140 | def testDbGetQuerySorting{ 141 | val rMongo = new RMongo("test") 142 | val results = rMongo.dbGetQuery("test_data", 143 | """ { "$query": {}, "$orderby": { "foo": -1 } }} """) 144 | val record = parsedFirstRecordFrom(results) 145 | 146 | Assert.assertEquals("n1", record.getOrElse("foo", "")) 147 | } 148 | 149 | @Test 150 | def testDbGetQueryPaginate{ 151 | val rMongo = new RMongo("test") 152 | val page1 = rMongo.dbGetQuery("test_data", """ {} """, 0, 1) 153 | val record1 = parsedFirstRecordFrom(page1) 154 | 155 | Assert.assertEquals("bar", record1.getOrElse("foo", "")) 156 | 157 | val page2 = rMongo.dbGetQuery("test_data", """ {} """, 1, 1) 158 | val record2 = parsedFirstRecordFrom(page2) 159 | 160 | Assert.assertEquals("n1", record2.getOrElse("foo", "")) 161 | } 162 | 163 | @Test 164 | def testToCsvOutput{ 165 | clearTestDB 166 | 167 | val rMongo = new RMongo("test") 168 | 169 | val doc = """ {"_id": "foo", "foo": "bar\n\r should not break", "size": 5} """ 170 | val response = rMongo.dbInsertDocument("test_data", doc) 171 | 172 | val query = "{}" 173 | val queryObject = JSON.parse(query).asInstanceOf[DBObject] 174 | val collection = db.getCollection("test_data") 175 | val cursor = collection.find(queryObject) 176 | 177 | val results = RMongo.toCsvOutput(cursor) 178 | 179 | assert(results != null) 180 | } 181 | 182 | def parsedFirstRecordFrom(results: String):Map[String, Any] = { 183 | val lines = results.split("\n").filter(_.size > 0) 184 | 185 | val keys = lines.head.split(RMongo.SEPARATOR ) 186 | val entries = lines.drop(1) 187 | val entry = entries.headOption.getOrElse("").split(RMongo.SEPARATOR ) 188 | 189 | keys.zip(entry).toMap 190 | } 191 | 192 | @Test 193 | def testDbGetDistinct{ 194 | val rMongo = new RMongo("test") 195 | val results = rMongo.dbGetDistinct("test_data", "size") 196 | 197 | Assert.assertEquals("\"5\"\n\"10\"", results) 198 | } 199 | 200 | @Test 201 | def testDbAggregate{ 202 | val rMongo = new RMongo("test") 203 | var pipeline = Array( 204 | """ { "$project" : { "baz" : "$foo" } } """, 205 | """ { "$group" : { "_id" : "$baz" } } """, 206 | """ { "$match" : { "_id" : "bar" } } """) 207 | val results = rMongo.dbAggregate("test_data", pipeline) 208 | 209 | //Assert.assertEquals("\"{ \"_id\" : \"bar\"}\"", results) 210 | assert(results.contains("{ \"_id\" : \"bar\"}")) 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /tests/RMongo-Ex.R: -------------------------------------------------------------------------------- 1 | Sys.setenv(NOAWT = "1") 2 | 3 | library("RUnit") 4 | library("RMongo") 5 | library('rJava') 6 | 7 | test.dbInsertDocument <- function(){ 8 | mongo <- mongoDbConnect("test") 9 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 10 | dbDisconnect(mongo) 11 | 12 | checkEquals("ok", output) 13 | } 14 | 15 | test.dbRemoveQuery <- function(){ 16 | mongo <- mongoDbConnect("test") 17 | output <- dbRemoveQuery(mongo, "test_data", '{}') 18 | dbDisconnect(mongo) 19 | 20 | checkEquals("ok", output) 21 | } 22 | 23 | test.dbGetQuery <- function(){ 24 | mongo <- mongoDbConnect("test") 25 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 26 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar with spaces"}') 27 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar with \n\r new lines"}') 28 | output <- dbGetQuery(mongo, 'test_data', '{"foo": { "$regex": "bar*", "$options": "i"} }') 29 | dbRemoveQuery(mongo, "test_data", '{}') 30 | dbDisconnect(mongo) 31 | checkEquals("bar", as.character(output[1,]$foo)) 32 | checkEquals("bar with spaces", as.character(output[2,]$foo)) 33 | checkEquals("bar with \n\r new lines", as.character(output[3,]$foo)) 34 | } 35 | 36 | test.dbGetQuerySkipAndLimit <- function(){ 37 | mongo <- mongoDbConnect("test") 38 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 39 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 40 | output <- dbGetQuery(mongo, "test_data", '{"foo": "bar"}', 0, 1) 41 | dbRemoveQuery(mongo, "test_data", '{}') 42 | dbDisconnect(mongo) 43 | checkEquals(1, length(output[output$foo == 'bar', 1])) 44 | } 45 | 46 | test.dbGetQuerySkip <- function(){ 47 | mongo <- mongoDbConnect("test") 48 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 49 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 50 | output <- dbGetQuery(mongo, "test_data", '{"foo": "bar"}', skip=1) 51 | dbRemoveQuery(mongo, "test_data", '{}') 52 | dbDisconnect(mongo) 53 | checkEquals(1, length(output[output$foo == 'bar', 1])) 54 | } 55 | test.dbGetQueryLimit <- function(){ 56 | mongo <- mongoDbConnect("test") 57 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 58 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 59 | output <- dbGetQuery(mongo, "test_data", '{"foo": "bar"}', limit=1) 60 | dbRemoveQuery(mongo, "test_data", '{}') 61 | dbDisconnect(mongo) 62 | checkEquals(1, length(output[output$foo == 'bar', 1])) 63 | } 64 | test.dbGetQueryWithEmptyCollection <- function(){ 65 | mongo <- mongoDbConnect('test') 66 | output <- dbGetQuery(mongo, 'test_data', '{"EMPTY": "EMPTY"}') 67 | dbRemoveQuery(mongo, "test_data", '{}') 68 | dbDisconnect(mongo) 69 | checkEquals(data.frame(), output) 70 | } 71 | 72 | test.dbGetQuerySorting <- function(){ 73 | #insert the records using r-mongo-scala project 74 | mongo <- mongoDbConnect("test") 75 | dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 76 | dbInsertDocument(mongo, "test_data", '{"foo": "newbar"}') 77 | 78 | output <- dbGetQuery(mongo, "test_data", '{ "$query": {}, "$orderby": { "foo": -1 } }}') 79 | dbRemoveQuery(mongo, "test_data", '{}') 80 | dbDisconnect(mongo) 81 | 82 | checkEquals("newbar", as.character(output[1,]$foo)) 83 | } 84 | 85 | test.dbGetQueryForKeys <- function(){ 86 | mongo <- mongoDbConnect("test") 87 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5}') 88 | results <- dbGetQueryForKeys(mongo, "test_data", '{"foo": "bar"}', '{"foo": 1}') 89 | dbRemoveQuery(mongo, "test_data", '{}') 90 | dbDisconnect(mongo) 91 | 92 | checkEquals(TRUE, any(names(results) == "foo")) 93 | checkEquals(TRUE, any(names(results) != "size")) 94 | } 95 | 96 | test.dbInsertStructured <- function(){ 97 | mongo <- mongoDbConnect("test") 98 | output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar", "structured": {"foo": "baz"}}') 99 | output <- dbGetQuery(mongo, "test_data", '{}') 100 | 101 | dbRemoveQuery(mongo, "test_data", '{}') 102 | dbDisconnect(mongo) 103 | checkEquals("{ \"foo\" : \"baz\"}", as.character(output[1,]$structured)) 104 | } 105 | 106 | test.dbGetDistinct <- function(){ 107 | mongo <- mongoDbConnect("test") 108 | dbInsertDocument(mongo, "test_data", '{"foo": "bar and baz"}') 109 | dbInsertDocument(mongo, "test_data", '{"foo": "baz and bar"}') 110 | 111 | output <- dbGetDistinct(mongo, "test_data", 'foo') 112 | dbRemoveQuery(mongo, "test_data", '{}') 113 | dbDisconnect(mongo) 114 | 115 | checkEquals("bar and baz", as.character(output[1])) 116 | checkEquals("baz and bar", as.character(output[2])) 117 | } 118 | 119 | test.dbAggregate <- function(){ 120 | mongo <- mongoDbConnect("test") 121 | dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5 }') 122 | dbInsertDocument(mongo, "test_data", '{"foo": "nl", "size": 10 }') 123 | 124 | output <- dbAggregate(mongo, "test_data", c(' { "$project" : { "baz" : "$foo" } } ', 125 | ' { "$group" : { "_id" : "$baz" } } ', 126 | ' { "$match" : { "_id" : "bar" } } ')) 127 | dbRemoveQuery(mongo, "test_data", '{}') 128 | dbDisconnect(mongo) 129 | 130 | # print(length(output)) 131 | print(as.character(output[1])) 132 | 133 | checkEquals("{ \"_id\" : \"bar\"}", as.character(output[1])) 134 | # checkEquals("bar", as.character(output[1])) 135 | } 136 | 137 | test.dbInsertDocument() 138 | test.dbRemoveQuery() 139 | test.dbGetQuery() 140 | test.dbGetQuerySkipAndLimit() 141 | test.dbGetQueryWithEmptyCollection() 142 | test.dbGetQuerySorting() 143 | test.dbGetQueryForKeys() 144 | test.dbInsertStructured() 145 | test.dbGetDistinct() 146 | test.dbAggregate() 147 | -------------------------------------------------------------------------------- /tests/RMongo-Ex.Rout.save: -------------------------------------------------------------------------------- 1 | 2 | R version 3.0.1 (2013-05-16) -- "Good Sport" 3 | Copyright (C) 2013 The R Foundation for Statistical Computing 4 | Platform: x86_64-apple-darwin10.8.0 (64-bit) 5 | 6 | R is free software and comes with ABSOLUTELY NO WARRANTY. 7 | You are welcome to redistribute it under certain conditions. 8 | Type 'license()' or 'licence()' for distribution details. 9 | 10 | R is a collaborative project with many contributors. 11 | Type 'contributors()' for more information and 12 | 'citation()' on how to cite R or R packages in publications. 13 | 14 | Type 'demo()' for some demos, 'help()' for on-line help, or 15 | 'help.start()' for an HTML browser interface to help. 16 | Type 'q()' to quit R. 17 | 18 | > library("RUnit") 19 | > library("RMongo") 20 | Loading required package: rJava 21 | > library('rJava') 22 | > 23 | > test.dbInsertDocument <- function(){ 24 | + mongo <- mongoDbConnect("test") 25 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 26 | + dbDisconnect(mongo) 27 | + 28 | + checkEquals("ok", output) 29 | + } 30 | > 31 | > test.dbRemoveQuery <- function(){ 32 | + mongo <- mongoDbConnect("test") 33 | + output <- dbRemoveQuery(mongo, "test_data", '{}') 34 | + dbDisconnect(mongo) 35 | + 36 | + checkEquals("ok", output) 37 | + } 38 | > 39 | > test.dbGetQuery <- function(){ 40 | + mongo <- mongoDbConnect("test") 41 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 42 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar with spaces"}') 43 | + output <- dbGetQuery(mongo, 'test_data', '{"foo": { "$regex": "bar*", "$options": "i"} }') 44 | + dbRemoveQuery(mongo, "test_data", '{}') 45 | + dbDisconnect(mongo) 46 | + checkEquals("bar", as.character(output[1,]$foo)) 47 | + checkEquals("bar with spaces", as.character(output[2,]$foo)) 48 | + } 49 | > 50 | > test.dbGetQuerySkipAndLimit <- function(){ 51 | + mongo <- mongoDbConnect("test") 52 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 53 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 54 | + output <- dbGetQuery(mongo, "test_data", '{"foo": "bar"}', 0, 1) 55 | + dbRemoveQuery(mongo, "test_data", '{}') 56 | + dbDisconnect(mongo) 57 | + checkEquals(1, length(output[output$foo == 'bar', 1])) 58 | + } 59 | > 60 | > test.dbGetQueryWithEmptyCollection <- function(){ 61 | + mongo <- mongoDbConnect('test') 62 | + output <- dbGetQuery(mongo, 'test_data', '{"EMPTY": "EMPTY"}') 63 | + dbRemoveQuery(mongo, "test_data", '{}') 64 | + dbDisconnect(mongo) 65 | + checkEquals(data.frame(), output) 66 | + } 67 | > 68 | > test.dbGetQuerySorting <- function(){ 69 | + #insert the records using r-mongo-scala project 70 | + mongo <- mongoDbConnect("test") 71 | + dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') 72 | + dbInsertDocument(mongo, "test_data", '{"foo": "newbar"}') 73 | + 74 | + output <- dbGetQuery(mongo, "test_data", '{ "$query": {}, "$orderby": { "foo": -1 } }}') 75 | + dbRemoveQuery(mongo, "test_data", '{}') 76 | + dbDisconnect(mongo) 77 | + 78 | + checkEquals("newbar", as.character(output[1,]$foo)) 79 | + } 80 | > 81 | > test.dbGetQueryForKeys <- function(){ 82 | + mongo <- mongoDbConnect("test") 83 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5}') 84 | + results <- dbGetQueryForKeys(mongo, "test_data", '{"foo": "bar"}', '{"foo": 1}') 85 | + dbRemoveQuery(mongo, "test_data", '{}') 86 | + dbDisconnect(mongo) 87 | + 88 | + checkEquals(TRUE, any(names(results) == "foo")) 89 | + checkEquals(TRUE, any(names(results) != "size")) 90 | + } 91 | > 92 | > test.dbInsertStructured <- function(){ 93 | + mongo <- mongoDbConnect("test") 94 | + output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar", "structured": {"foo": "baz"}}') 95 | + output <- dbGetQuery(mongo, "test_data", '{}') 96 | + 97 | + dbRemoveQuery(mongo, "test_data", '{}') 98 | + dbDisconnect(mongo) 99 | + checkEquals("{ \"foo\" : \"baz\"}", as.character(output[1,]$structured)) 100 | + } 101 | > 102 | > test.dbGetDistinct <- function(){ 103 | + mongo <- mongoDbConnect("test") 104 | + dbInsertDocument(mongo, "test_data", '{"foo": "bar and baz"}') 105 | + dbInsertDocument(mongo, "test_data", '{"foo": "baz and bar"}') 106 | + 107 | + output <- dbGetDistinct(mongo, "test_data", 'foo') 108 | + dbRemoveQuery(mongo, "test_data", '{}') 109 | + dbDisconnect(mongo) 110 | + 111 | + checkEquals("bar and baz", as.character(output[1])) 112 | + checkEquals("baz and bar", as.character(output[2])) 113 | + } 114 | > 115 | > test.dbAggregate <- function(){ 116 | + mongo <- mongoDbConnect("test") 117 | + dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5 }') 118 | + dbInsertDocument(mongo, "test_data", '{"foo": "nl", "size": 10 }') 119 | + 120 | + output <- dbAggregate(mongo, "test_data", c(' { "$project" : { "baz" : "$foo" } } ', 121 | + ' { "$group" : { "_id" : "$baz" } } ', 122 | + ' { "$match" : { "_id" : "bar" } } ')) 123 | + dbRemoveQuery(mongo, "test_data", '{}') 124 | + dbDisconnect(mongo) 125 | + 126 | + # print(length(output)) 127 | + print(as.character(output[1])) 128 | + 129 | + checkEquals("{ \"_id\" : \"bar\"}", as.character(output[1])) 130 | + # checkEquals("bar", as.character(output[1])) 131 | + } 132 | > 133 | > test.dbInsertDocument() 134 | [1] TRUE 135 | > test.dbRemoveQuery() 136 | [1] TRUE 137 | > test.dbGetQuery() 138 | [1] TRUE 139 | > test.dbGetQuerySkipAndLimit() 140 | [1] TRUE 141 | > test.dbGetQueryWithEmptyCollection() 142 | [1] TRUE 143 | > test.dbGetQuerySorting() 144 | [1] TRUE 145 | > test.dbGetQueryForKeys() 146 | [1] TRUE 147 | > test.dbInsertStructured() 148 | [1] TRUE 149 | > test.dbGetDistinct() 150 | [1] TRUE 151 | > test.dbAggregate() 152 | [1] "{ \"_id\" : \"bar\"}" 153 | [1] TRUE 154 | > 155 | > proc.time() 156 | user system elapsed 157 | 1.485 0.077 1.293 158 | --------------------------------------------------------------------------------