博客
关于我
Monogodb 分组操作
阅读量:793 次
发布时间:2023-02-09

本文共 3176 字,大约阅读时间需要 10 分钟。

方法一:

/** 业务逻辑 */

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;
}

转载于:https://www.cnblogs.com/xiaohaizhuimeng/p/mongoDB.html

你可能感兴趣的文章
luogu P1268 树的重量
查看>>
LUOGU P4095 [HEOI2013]Eden 的新背包问题
查看>>
Luogu2973:[USACO10HOL]赶小猪
查看>>
LVS-DR工作原理图文详解
查看>>
LVS精益价值管理系统 LVS.Web.ashx SQL注入漏洞复现
查看>>
LZ4 1.10 压缩算法发布!具有多线程功能,压缩速度显著提高达 8 倍
查看>>
lzg_ad:打印机需要的组件支持
查看>>
mabatis 中出现&lt; 以及&gt; 代表什么意思?
查看>>
Mac book air 重新安装系统验证显示 untrusted_cert_title
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
mac elasticsearch brew安装填坑
查看>>
mac M1 下安装docker 及相关镜像
查看>>
Mac M1 芯片不兼容node-sass
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
Mac OS下错误The superclass “javax.servlet.http.HttpServlet“ was not found on the Java Build Path的解决方法
查看>>
mac 搭建APK反编译环境[转]
查看>>
MAC 显示隐藏文件
查看>>
mac 虚拟机安装oracle 11g,MAC+Vmware Fusion安装Oracle11g RAC
查看>>