? group
这个确实挺高级的,查看示例:
db.coll.group(
{key: { a: , b: }, true true
cond: { active:1 },
reduce: function(obj,prev) { prev.csum += obj.c; },
initial: { csum: 0 }
});
API:
public DBObject group(DBObject key,
DBObject cond,
DBObject initial,
String reduce)
throws MongoExceptionParameters:
key - - { a : true }
cond - - optional condition on query
reduce - javascript reduce function
initial - initial value for first match on a key
JAVA中应用例子:
System.out.println("-----------华丽分隔线---------------");
System.out.println("SELECT sum(isGold)
goldCount,discipline,gender,event FROM schedule
where isGold=1 group by discipline,gender,event");
startTime = System.currentTimeMillis();
//first group by key
BasicDBObject key = new BasicDBObject();
key.put("discipline", true);
key.put("gender", true);
key.put("event", true);
//second where cond
BasicDBObject cond = new BasicDBObject();
cond.put("isGold", 1);
//third initial
BasicDBObject initial = new BasicDBObject();
initial.put("goldCount", 0);
//four String reduce
String reduce = "function(obj,prev) { prev.goldCount +=
obj.isGold; }";
query = new BasicDBObject();
field = new BasicDBObject();
field.put("rsc",true);
field.put("startDate",true);
field.put("startTime",true);
field.put("competitionStatus",true);
sort = new BasicDBObject();
sort.put("startDate", 1);
sort.put("startTime", 1);
DBObject obj = coll.group(key, cond, initial, reduce);
//cur = coll.find(query,field).sort(sort);
//System.out.println("查询获得的长度"+cur.count());
System.out.println("查询耗时:"+(System.currentTimeMillis()-startTime)+"毫秒.");
Set set = obj.keySet();
Iterator it = set.iterator();
int i=0;
while(it.hasNext()){
String str_key = (String) it.next();
System.out.println(obj.get(str_key));
i++;
}
System.out.println("i========"+i);
运行结果:
-----------华丽分隔线---------------
SELECT sum(isGold) goldCount,discipline,gender,event FROM schedule where isGold=1 group by discipline
查询耗时:63毫秒.
{ "discipline" : "AR" , "goldCount" : 4.0}
{ "discipline" : "AT" , "goldCount" : 45.0}
{ "discipline" : "BB" , "goldCount" : 1.0}
{ "discipline" : "BD" , "goldCount" : 5.0}
{ "discipline" : "BK" , "goldCount" : 2.0}
{ "discipline" : "BV" , "goldCount" : 2.0}
{ "discipline" : "BX" , "goldCount" : 11.0}
{ "discipline" : "CB" , "goldCount" : 2.0}
{ "discipline" : "CF" , "goldCount" : 12.0}
{ "discipline" : "CM" , "goldCount" : 2.0}
{ "discipline" : "CR" , "goldCount" : 4.0}
{ "discipline" : "CS" , "goldCount" : 4.0}
{ "discipline" : "CT" , "goldCount" : 8.0}
{ "discipline" : "DV" , "goldCount" : 8.0}
{ "discipline" : "EQ" , "goldCount" : 6.0}
{ "discipline" : "FB" , "goldCount" : 2.0}
{ "discipline" : "FE" , "goldCount" : 10.0}
{ "discipline" : "GA" , "goldCount" : 14.0}
{ "discipline" : "GR" , "goldCount" : 2.0}
{ "discipline" : "GT" , "goldCount" : 2.0}
{ "discipline" : "HB" , "goldCount" : 2.0}
{ "discipline" : "HO" , "goldCount" : 2.0}
{ "discipline" : "JU" , "goldCount" : 14.0}
{ "discipline" : "RO" , "goldCount" : 14.0}
{ "discipline" : "SA" , "goldCount" : 11.0}
{ "discipline" : "SH" , "goldCount" : 15.0}
{ "discipline" : "SO" , "goldCount" : 1.0}
{ "discipline" : "SW" , "goldCount" : 34.0}
{ "discipline" : "SY" , "goldCount" : 2.0}
{ "discipline" : "TE" , "goldCount" : 4.0}
{ "discipline" : "TK" , "goldCount" : 8.0}
{ "discipline" : "TR" , "goldCount" : 2.0}
{ "discipline" : "TT" , "goldCount" : 4.0}
{ "discipline" : "VO" , "goldCount" : 2.0}
{ "discipline" : "WP" , "goldCount" : 2.0}
{ "discipline" : "WR" , "goldCount" : 18.0}
i========36