Permalink
Browse files

Added methods for removing data from collections and to list all uniq…

…ue values.
  • Loading branch information...
1 parent bfb9032 commit b6b61d3f3f153a3269fed80329f2bc2e81b0b92d Luca Di Gaspero committed Oct 10, 2011
View
@@ -1,4 +1,4 @@
import('rJava')
export('mongoDbConnect')
exportClass('RMongo')
-exportMethods('dbInsertDocument', 'dbAuthenticate', 'dbShowCollections', 'dbGetQuery', 'dbGetQueryForKeys', 'dbDisconnect')
+exportMethods('dbInsertDocument', 'dbAuthenticate', 'dbShowCollections', 'dbGetQuery', 'dbGetQueryForKeys', 'dbDisconnect', 'dbRemoveQuery', 'dbGetDistinct')
View
@@ -75,3 +75,32 @@ setMethod("dbDisconnect", signature(rmongo.object="RMongo"),
}
)
+setGeneric("dbRemoveQuery", function(rmongo.object, collection, query) standardGeneric("dbRemoveQuery"))
+setMethod("dbRemoveQuery", signature(rmongo.object="RMongo", collection="character", query="character"),
+ function(rmongo.object, collection, query){
+ results <- .jcall(rmongo.object@javaMongo, "S", "dbRemoveQuery", collection, query)
+ results
+ }
+)
+
+setGeneric("dbGetDistinct", function(rmongo.object, collection, key, query="") standardGeneric("dbGetDistinct"))
+setMethod("dbGetDistinct", signature(rmongo.object="RMongo", collection="character", key="character", query="missing"),
+ function(rmongo.object, collection, key, query=""){
+ dbGetDistinct(rmongo.object, collection, key, "")
+ }
+)
+
+setMethod("dbGetDistinct", signature(rmongo.object="RMongo", collection="character", key="character", query="character"),
+ function(rmongo.object, collection, key, query){
+ results <- .jcall(rmongo.object@javaMongo, "S", "dbGetDistinct", collection, key, query)
+ if(results == ""){
+ vector(mode="character")
+ }else{
+ con <- textConnection(results)
+ data.frame.results <- read.table(con, sep="\n", stringsAsFactors=FALSE, quote="\"", header=FALSE)
+ close(con)
+
+ as.vector(t(data.frame.results))
+ }
+ }
+)
Binary file not shown.
View
@@ -12,6 +12,8 @@ Contains the mongodb connection information. See mongoDbConnect for creating a R
\item{dbGetQuery}{}
\item{dbGetQueryForKeys}{}
\item{dbInsertDocument}{}
+ \item{dbRemoveQuery}{}
+ \item{dbGetDistinct}{}
\item{dbDisconnect}{}
}
}
@@ -1,36 +0,0 @@
-\name{dbGetQueryForKeys-methods}
-\docType{methods}
-\alias{dbGetQueryForKeys}
-\alias{dbGetQueryForKeys-methods}
-\alias{dbGetQueryForKeys,RMongo,character,character,character,missing,missing-method}
-\alias{dbGetQueryForKeys,RMongo,character,character,character,numeric,numeric-method}
-
-\title{Performing a MongoDB query and return only a subset of the keys}
-
-\description{
-Send a json query to mongodb.
-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.
-}
-
-\usage{
- dbGetQueryForKeys(rmongo.object, collection, query, keys, skip=0, limit=1000)
-}
-
-\arguments{
- \item{rmongo.object}{The RMongo object.}
- \item{collection}{The name of the collection the query is being performed upon.}
- \item{query}{A JSON string query. See http://www.mongodb.org/display/DOCS/Advanced+Queries for more information on the MongoDB query language.}
- \item{keys}{Specify a set of keys to return.}
- \item{skip}{Offset the resultset. Can be used with limit to perform pagination.}
- \item{limit}{Limits the resultset size.}
-}
-
-\seealso{
- \code{\link{mongoDbConnect}}
-}
-\examples{
- mongo <- mongoDbConnect("test")
- output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}')
- results <- dbGetQueryForKeys(mongo, "test_data", '{"foo":"bar"}', '{"foo": 1}', 0, 100)
-}
-
@@ -30,6 +30,10 @@ The output is a data.frame object and will work properly only if the mongoDB col
}
\examples{
mongo <- mongoDbConnect("test")
- output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}')
+ output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}')
output <- dbGetQuery(mongo, 'test_data', '{"foo": "bar"}')
+ print(output)
+
+ output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar with spaces"}')
+ output <- dbGetQuery(mongo, 'test_data', '{"foo": { "$regex": "bar*", "$options": "i"} }')
}
@@ -72,7 +72,7 @@ class RMongo(dbName: String, host: String, port: Int) {
dbGetQuery(collectionName, query, keys, skipNum.toInt, limitNum.toInt)
}
- def dbGetQuery(collectionName: String, query: String, keys:String,
+ def dbGetQuery(collectionName: String, query: String, keys: String,
skipNum:Int, limitNum:Int): String = {
val dbCollection = db.getCollection(collectionName)
@@ -83,7 +83,26 @@ class RMongo(dbName: String, host: String, port: Int) {
val results = RMongo.toCsvOutput(cursor)
results
- }
+ }
+
+ def dbGetDistinct(collectionName: String, key: String): String = {
+ dbGetDistinct(collectionName, key, "")
+ }
+
+ def dbGetDistinct(collectionName: String, key: String, query: String): String = {
+ val dbCollection = db.getCollection(collectionName)
+
+ val queryObject = JSON.parse(query).asInstanceOf[DBObject]
+ val distinctResults = dbCollection.distinct(key, queryObject).iterator
+ val results = ListBuffer[String]()
+
+ while (distinctResults.hasNext) {
+ val item = distinctResults.next
+ results.append("\"" + item.toString.replaceAll("\n", "\\n") + "\"")
+ }
+
+ results.mkString("\n")
+ }
def close() {
m.close()
@@ -166,4 +166,12 @@ class MongoTest{
keys.zip(entry).toMap
}
+
+ @Test
+ def testDbGetDistinct{
+ val rMongo = new RMongo("test")
+ val results = rMongo.dbGetDistinct("test_data", "size")
+
+ Assert.assertEquals("\"5\"\n\"10\"", results)
+ }
}
View
@@ -10,26 +10,39 @@ test.dbInsertDocument <- function(){
checkEquals("ok", output)
}
+test.dbRemoveQuery <- function(){
+ mongo <- mongoDbConnect("test")
+ output <- dbRemoveQuery(mongo, "test_data", '{}')
+ dbDisconnect(mongo)
+
+ checkEquals("ok", output)
+}
+
test.dbGetQuery <- function(){
mongo <- mongoDbConnect("test")
output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}')
- output <- dbGetQuery(mongo, "test_data", '{"foo": "bar"}')
+ output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar with spaces"}')
+ output <- dbGetQuery(mongo, 'test_data', '{"foo": { "$regex": "bar*", "$options": "i"} }')
+ dbRemoveQuery(mongo, "test_data", '{}')
dbDisconnect(mongo)
checkEquals("bar", as.character(output[1,]$foo))
+ checkEquals("bar with spaces", as.character(output[2,]$foo))
}
test.dbGetQuerySkipAndLimit <- function(){
mongo <- mongoDbConnect("test")
output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}')
output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}')
output <- dbGetQuery(mongo, "test_data", '{"foo": "bar"}', 0, 1)
+ dbRemoveQuery(mongo, "test_data", '{}')
dbDisconnect(mongo)
checkEquals(1, length(output[output$foo == 'bar', 1]))
}
test.dbGetQueryWithEmptyCollection <- function(){
mongo <- mongoDbConnect('test')
output <- dbGetQuery(mongo, 'test_data', '{"EMPTY": "EMPTY"}')
+ dbRemoveQuery(mongo, "test_data", '{}')
dbDisconnect(mongo)
checkEquals(data.frame(), output)
}
@@ -41,6 +54,7 @@ test.dbGetQuerySorting <- function(){
dbInsertDocument(mongo, "test_data", '{"foo": "newbar"}')
output <- dbGetQuery(mongo, "test_data", '{ "$query": {}, "$orderby": { "foo": -1 } }}')
+ dbRemoveQuery(mongo, "test_data", '{}')
dbDisconnect(mongo)
checkEquals("newbar", as.character(output[1,]$foo))
@@ -50,6 +64,7 @@ test.dbGetQueryForKeys <- function(){
mongo <- mongoDbConnect("test")
output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5}')
results <- dbGetQueryForKeys(mongo, "test_data", '{"foo": "bar"}', '{"foo": 1}')
+ dbRemoveQuery(mongo, "test_data", '{}')
dbDisconnect(mongo)
checkEquals(TRUE, any(names(results) == "foo"))
@@ -58,17 +73,33 @@ test.dbGetQueryForKeys <- function(){
test.dbInsertStructured <- function(){
mongo <- mongoDbConnect("test")
- output <- dbInsertDocument(mongo, "test_data_s", '{"foo": "bar", "structured": {"foo": "baz"}}')
- output <- dbGetQuery(mongo, "test_data_s", '{}')
+ output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar", "structured": {"foo": "baz"}}')
+ output <- dbGetQuery(mongo, "test_data", '{}')
+ dbRemoveQuery(mongo, "test_data", '{}')
dbDisconnect(mongo)
checkEquals("{ \"foo\" : \"baz\"}", as.character(output[1,]$structured))
}
+test.dbGetDistinct <- function(){
+ mongo <- mongoDbConnect("test")
+ dbInsertDocument(mongo, "test_data", '{"foo": "bar and baz"}')
+ dbInsertDocument(mongo, "test_data", '{"foo": "baz and bar"}')
+
+ output <- dbGetDistinct(mongo, "test_data", 'foo')
+ dbRemoveQuery(mongo, "test_data", '{}')
+ dbDisconnect(mongo)
+
+ checkEquals("bar and baz", as.character(output[1]))
+ checkEquals("baz and bar", as.character(output[2]))
+}
+
test.dbInsertDocument()
+test.dbRemoveQuery()
test.dbGetQuery()
test.dbGetQuerySkipAndLimit()
test.dbGetQueryWithEmptyCollection()
test.dbGetQuerySorting()
test.dbGetQueryForKeys()
test.dbInsertStructured()
+test.dbGetDistinct()

0 comments on commit b6b61d3

Please sign in to comment.