最近在做mongodb的分析统计,发现group分组有多种做法,和sql还真是不太样,需要写点js代码,直接贴示例代码:
这是我的collection,名字:t_user_score
public class UserScore implements Serializable {
…
private Integer userId;
private BigDecimal userScore;
…
}
int userCount = 0;
GroupBy groupBy = GroupBy.key("userId")
.initialDocument("userScoreTotal:0,userScoreCount:0,userScoreAvg:0}")
.reduceFunction("function(doc, prev){ if(doc.userScore != null){"
+"prev.userScoreTotal += parseFloat(doc.userScore);"
+"prev.userScoreCount + =1;"
+"}"
+"}")
.finalizeFunction("function(prev){ if(prev.userScoreCount > 0) {"
+"prev.userScoreAvg=prev.userScoreTotal / prev.userScoreCount;"
+"}"
+"}");
GroupByResults<UserScore> r = evaluationTemplate.group("t_user_score", groupBy, UserScore.class);
BasicDBList list = (BasicDBList)r.getRawResults().get("retval");
for (int i = 0; i < list.size(); i ++) {
BasicDBObject obj = (BasicDBObject)list.get(i);
int userId = Double.valueOf(String.valueOf(obj.get("userId"))).intValue();
logger.info(String.format("userId=%s json=%s ", userId, Utils.toJSONString(obj)));
}
登录控制台,建唯一索引(后台创建唯一的复合索引):
db.t_user_score.ensureIndex({“userId”:1},{background:1,unique:1});
–查看索引
db.t_user_score.getIndexes()
–查看索引大小
db.t_user_score.totalIndexSize();
–重建索引
db.t_user_score.reIndex()
–删除索引
–db.t_user_score.dropIndex(“id_1”)
–使用索引的结果
db.t_user_score.find({“userId”:1000}).explain();
更多查询优化:AnalyzeQuery Performance :http://docs.mongodb.org/manual/tutorial/analyze-query-plan/