javascript - MongoDB mapreduce而不是聚合
问题描述
我是 mongodb 中的 MR 工作的新手。我有一个聚合函数,如下所示:
db.acollection.aggregate([{$match:{ "userId" : { "$eq" : "raghu" }}},
{$group:{ "_id" : { "region":"$region", "shipMode" : "$shipMode"}, "sales" : { "$sum" : "$sales"}}},
{"$sort" : { "_id.region" : 1, "sales" : 1 }}, { "$limit" : 1000}]);
由于性能问题参考:MongoDB 在负载下执行缓慢我正在创建一个 MRjob。所以我应该得到所有相关的文件,Map
所有的文件Reduce
应该是groupby
,我猜。我有如下功能:sort
limit
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;
}
不确定如何获得 JavaScript 代码,我想将完整的 JSON 对象作为值发出,以便可以 mapreduce 它。就像是 :
private String getMapFunction() {
//somecode here and then finally ..
emit(this.tenantId_V, object);
}
解决方案
推荐阅读
- java - 无需标记或编译的 Maven 发布
- fish - 鱼壳:提示更改为'>'
- node.js - 为什么heroku无法在项目上编译构建?
- java - 赋值时XML中的setter注入不起作用
- ios - 如何从 Swift 4 中的另一个类正确调用函数(其中包含 IBOutlet 变量)
- angularjs - How to filter the data on array and remove some data in angular
- c# - Unity 中有 MultiKeyDictionary 吗?
- python - 在 Python 中读取文件行时如何不创建无限循环?
- video - 修改FPS引起的异常
- java - 滚动视图顶部的静态按钮 android studio