学习文档:http://www.makaidong.com/博客园牛/33875.shtml
code:
package com.focus.wechat.demo;
import com.focus.commonutil.db.hbs.MongoDBManager;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
public class GroupDemo {
public static void main(String[] args) {
group();
}
public static void group(){
DBCollection collection = MongoDBManager.getCollection("Gao_test2");
DBObject key = new BasicDBObject();
key.put("age", true);
key.put("city", true);
DBObject initial = new BasicDBObject();
DBObject index = new BasicDBObject();
// index.put("city", "");
index.put("count", 0);
initial.put("user", index);
String reduce = "function (doc, out) { "
+ " out.user.count = out.user.count+=out.age; "
// + " out.user.city = doc.city;"
+ "}";
DBObject connObject = new BasicDBObject();
BasicDBList group = (BasicDBList) collection.group(key, connObject, initial, reduce);
System.out.println(group.toString());
}
}