您好,我想统计某个大类下面的小的数据情况,并按小类排序,SQL语句为
select smallType,count(*) from table group by smallType order by smallType
BSONObject index = null;
// DBCursor indexCursor = cl.getIndex(Constants.INDEX_NAME);
// if (indexCursor.hasNext())
// index = indexCursor.getNext();
// result cursor
DBCursor dataCursor = null;
// query condition
BSONObject query = new BasicBSONObject();
// BSONObject condition = new BasicBSONObject();
//condition.put("$gte", 0);
// condition.put("$lte", 9);
// query.put("Id", condition);
// return fields
BSONObject selector = new BasicBSONObject();
BSONObject groupItem = new BasicBSONObject();
groupItem.put("_id", "$smallType");//这里的group 怎么写的?
BSONObject totalItem = new BasicBSONObject();
totalItem.put("$sum", 1);
groupItem.put("count", totalItem);//这里的group 怎么写的?
selector.put("$group", groupItem);
List list = new ArrayList();
//list.add(matcherBson); // 添加查询
list.add(selector); //添加查询字段
list.add(query); //添加查询字段
//selector.put("_id", null);
DBCursor cursor = cl.aggregate(list);
// order by ASC(1)/DESC(-1)
//BSONObject orderBy = new BasicBSONObject();
// orderBy.put("_id", -1);
// dataCursor = cl.query(query, selector, orderBy, null, 0, -1);
// or
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}", "{'Id':null,'Age':null}", "{'Id':-1}", null, 0, -1);
// or
// dataCursor = cl.query("{'Id':{'$gte':0,'$lte':9}}", "{'Id':null,'Age':null}", "{'Id':-1}", index, 0, -1);
// operate data by cursor
while (cursor.hasNext()) {
System.out.println(cursor.getNext());
}
我这样写法,程序执行就一直不动了,数据总量才千万级的,smallType及bigType都做了索引的,大类中有一类数据,基本不用统计,谢谢