Problem with Map Reduce Java API

50 views
Skip to first unread message

Ravi Dontharaju

unread,
Dec 8, 2011, 7:51:17 PM12/8/11
to mongodb-user
Hi,

I am trying the following mapReduce operation in my file.

public DBCursor getCount(
String collectionName,
String mapStringOne,
String mapStringTwo) {

String map =
String.format("function() { " +
"emit({%s: this.%s, %s: this.%s}, " +
"{count: this.count});" +
"};",
mapStringOne,
mapStringOne,
mapStringTwo,
mapStringTwo);
String reduce = "function(key, values) { " +
"var count = 0;" +
"values.forEach(function(doc) {" +
"count += doc['count'];" +
"});" +
"return {count: count};" +
"};";

MapReduceOutput mapReduceOutput =
collection.mapReduce(
map, reduce,
"DummyCollection", null);

return mapReduceOutput.results();
}

The Java Doc says that for the method "mapReduce", "outputCollection"
is optional - leave null if want to use temp collection. (3rd
parameter in the mapReduce method).

However, when I try to pass null as an argument, I get the following
exception:
com.mongodb.MongoException: mapreduce failed: { "assertion" : "'out'
has to be a string or an object" , "assertionCode" : 13606 ,
"errmsg" : "db assertion failure" , "ok" : 0.0}
at com.mongodb.DBCollection.mapReduce(DBCollection.java:577)
at com.mongodb.DBCollection.mapReduce(DBCollection.java:568)
at com.mongo.bridge.MongoBridge.getCount(MongoBridge.java:296)

My situation is that I don't want to create a new collection for my
mapReduce output, but in my current scenario, it creates a collection
called "DummyCollection" with the mapReduce output as the documents in
it.

Any idea how I can avoid this?
The mongodb binary version is 2.0.0.

Thanks,
Ravi Dontharaju

Brendan W. McAdams

unread,
Dec 8, 2011, 8:06:34 PM12/8/11
to mongod...@googlegroups.com

Output has been a required argument since MongoDB 1.8.0.

If you want to skip tlsaving the output you can specify { inline: 1 }

See http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Outputoptions

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Reply all
Reply to author
Forward
0 new messages