博客
关于我
Monogodb 分组操作
阅读量:798 次
发布时间: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

你可能感兴趣的文章
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>