Permalink
Browse files

Failing to get connection between R and Scala

  • Loading branch information...
1 parent 422641c commit ace93f80f4e40252c66173ffc9794b1a3aff3f25 @jayjacobs jayjacobs committed Jan 4, 2013
View
@@ -0,0 +1,36 @@
+\name{dbAggregate-methods}
+\docType{methods}
+\alias{dbAggregate}
+\alias{dbAggregate-methods}
+\alias{dbAggregate,RMongo,character,character-method}
+
+\title{Performing a MongoDB aggregate query}
+
+\description{
+Send a set of aggregate pipeline commands 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{
+ dbAggregate(rmongo.object, collection, query)
+}
+
+\arguments{
+ \item{rmongo.object}{The RMongo object.}
+ \item{collection}{The name of the collection the query is being performed upon.}
+ \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.}
+}
+
+\seealso{
+ \code{\link{mongoDbConnect}}
+}
+\examples{
+ mongo <- mongoDbConnect("test")
+ # insert two records
+ dbInsertDocument(mongo, "test_data", '{"foo": "bar", "size": 5 }')
+ dbInsertDocument(mongo, "test_data", '{"foo": "nl", "size": 10 }')
+ output <- dbAggregate(mongo, "test_data", c(' { "$project" : { "baz" : "$foo" } } ',
+ ' { "$group" : { "_id" : "$baz" } } ',
+ ' { "$match" : { "_id" : "bar" } } '))
+ print(output)
+}
@@ -6,6 +6,7 @@ import collection.JavaConversions._
import com.mongodb._
import java.util.HashSet
import java.util.Iterator
+import java.util.Arrays
/**
*
@@ -106,25 +107,14 @@ class RMongo(dbName: String, host: String, port: Int) {
results.mkString("\n")
}
- def dbAggregate(collectionName: String, queries: String*): String = {
+ def dbAggregate(collectionName: String, queries: Array[String]): String = {
val dbCollection = db.getCollection(collectionName)
val queryArray = new Array[DBObject](queries.length)
for ( i <- 0 to (queries.length - 1) ) {
val query = queries(i)
queryArray(i) = JSON.parse(query).asInstanceOf[DBObject]
}
- var aggregateIterator = dbCollection.aggregate(queryArray(0)).results.iterator
- if (queryArray.length == 2) {
- aggregateIterator = dbCollection.aggregate(queryArray(0), queryArray(1)).results.iterator
- } else if (queryArray.length == 3) {
- aggregateIterator = dbCollection.aggregate(queryArray(0), queryArray(1), queryArray(2)).results.iterator
- } else if (queryArray.length == 4) {
- aggregateIterator = dbCollection.aggregate(queryArray(0), queryArray(1), queryArray(2), queryArray(3)).results.iterator
- } else if (queryArray.length == 5) {
- aggregateIterator = dbCollection.aggregate(queryArray(0), queryArray(1), queryArray(2), queryArray(3), queryArray(4)).results.iterator
- } else if (queryArray.length == 6) {
- aggregateIterator = dbCollection.aggregate(queryArray(0), queryArray(1), queryArray(2), queryArray(3), queryArray(4), queryArray(5)).results.iterator
- }
+ var aggregateIterator = dbCollection.aggregate(queryArray(0), Arrays.copyOfRange(queryArray, 1, queryArray.length):_*).results.iterator
val results = ListBuffer[String]()
while (aggregateIterator.hasNext) {
@@ -178,10 +178,11 @@ class MongoTest{
@Test
def testDbAggregate{
val rMongo = new RMongo("test")
- val results = rMongo.dbAggregate("test_data",
+ var pipeline = Array(
""" { "$project" : { "baz" : "$foo" } } """,
""" { "$group" : { "_id" : "$baz" } } """,
""" { "$match" : { "_id" : "bar" } } """)
+ val results = rMongo.dbAggregate("test_data", pipeline)
Assert.assertEquals("\"{ \"_id\" : \"bar\"}\"", results)
}

0 comments on commit ace93f8

Please sign in to comment.