Monogodb 分组操作

方法一:

/** 业务逻辑 */
BasicDBObject query=new BasicDBObject();
query.put("state", "3");
JSONArray jsonarray=dao.getGroupCountWhere(query, "yl_dormitory");

for(int i=0;i<jsonarray.size();i++){
JSONObject jsob=jsonarray.getJSONObject(i);
String ment_id=jsob.getString("ment_id");
String count=jsob.getString("count");
//JSONArray jarment = dao.find(new BasicDBObject("_id", new ObjectId(ment_id)), "ment");
//JSONObject jsoba = jarapartment.getJSONObject(0);
//count=jsoba.get("count").toString();
dao.update(new BasicDBObject("_id",new ObjectId(ment_id)),
new BasicDBObject("count",String.valueOf(count))
.append("update_time", System.currentTimeMillis() / 1000),"ment");
}

 

/**
* 加条件的聚合统计
* @param doc
* @param collection
* @return
*/
public JSONArray getGroupCountWhere(BasicDBObject basicOB, String collection){
List<DBObject> list = new ArrayList<DBObject>();
JSONArray appendDor_userJar = new JSONArray();
try{
db = mg.getDB(DATABASENAME);
// 条件
DBObject match = new BasicDBObject("$match", basicOB);
// 利用$project拼装group需要的数据
DBObject fields = new BasicDBObject("ment_id", 1);
fields.put("_id", 1);
DBObject project = new BasicDBObject("$project", fields);

BasicDBObject groupFilters = new BasicDBObject("_id", "$ment_id");
groupFilters.put("count", new BasicDBObject("$sum", 1));
// 利用$group进行分组
BasicDBObject group = new BasicDBObject("$group", groupFilters);
// 按_id(即day)升序排列
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
// 取前N条数据 并计算没条数据的
// DBObject limit = new BasicDBObject("$limit", n);
List<DBObject> pipeline = Arrays.asList(match, project, group, sort);
AggregationOutput output = db.getCollection(collection)
.aggregate(pipeline);
// 计算
Iterable<DBObject> it = output.results();
Iterator<DBObject> itor = it.iterator();
while (itor.hasNext()) {
DBObject map=itor.next();
JSONObject appendDor_userJsob = new JSONObject();
if(null!=map.get("_id") && !map.get("_id").equals("")){
appendDor_userJsob.put("count",map.get("count"));
appendDor_userJsob.put("id",map.get("_id"));
appendDor_userJar.add(appendDorJsob);
}
System.out.println("count:"+map.get("count")+":"+map.get("_id"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(db!=null){
db=null;
}
}
return appendDor_userJar;
}

 

 

 

 

方法二:

 /** 业务逻辑 */
  Document group = new Document();
  Document groupData = new Document();
  groupData.append("_id", "$apartment_id");
  groupData.append("dormitory_count", new BasicDBObject("$sum", 1));
  group.append("$group", groupData);
  MongoDBDao dao = MongoDBDao.getMongoDBDaoInstance();
  List<Document> list = dao.getGroupCount(group, "yl_dormitory");
  JSONArray jsonarray=JSONArray.fromObject(list);

 

 

 

/**
* 聚合统计
* @param doc
* @param collection
* @return
*/
public List<Document> getGroupCount(Document doc, String collection){
MongoDatabase database = mg.getDatabase(DATABASENAME);
MongoCollection<Document> coll = database.getCollection(collection);

List<Bson> groups = new ArrayList<Bson>();
groups.add(doc);
MongoCursor<Document> cursor = coll.aggregate(groups).iterator();
List<Document> list = new ArrayList<Document>();
while (cursor.hasNext()) {
list.add(cursor.next());
}
return list;
}

你可能感兴趣的:(Monogodb 分组操作)