I am new to MR jobs in mongodb. I have a aggregate function looks like :
db.acollection.aggregate([{$match:{ "userId" : { "$eq" : "raghu" }}},
{$group:{ "_id" : { "region":"$region", "shipMode" : "$shipMode"}, "sales" : { "$sum" : "$sales"}}},
{"$sort" : { "_id.region" : 1, "sales" : 1 }}, { "$limit" : 1000}]);
Due to performance issues ref:MongoDB performing slow under load I am creating a MRjob. So I should get all the documents relevant in Map
and all the documents in Reduce
should be groupby
, sort
and limit
I guess. I have a function like below :
final MongoDatabase mongoDatabase = MongoUtils.getMongoDatabase(model);
BasicDBObject obj = pipeline.get(1);
MapReduceIterable<Document> list = mongoDatabase.getCollection(collectionName).mapReduce(getMapFunction(obj.getString("userId")), getReduceFunction());
// the above code is the main call but am mostly thinking about the map and reduce functions.
private String getMapFunction(String whereCondition) {
StringBuilder map= new StringBuilder();
map.append("function() {"
+ "var key=whereCondition;"
+ "if(this.userId==key)"
// how to get all the documents for this key ?
+ "}");
}
private String getReduceFunction() {
String reduce="";
// what should go here ?
return reduce;
}
Not sure how I can arrive at JavaScript code, I want to emit the complete JSON object as value so that I can mapreduce it. Something like :
private String getMapFunction() {
//somecode here and then finally ..
emit(this.tenantId_V, object);
}