博客
关于我
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

你可能感兴趣的文章
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>
MySQL 中随机抽样:order by rand limit 的替代方案
查看>>
MySQL 为什么需要两阶段提交?
查看>>
mysql 为某个字段的值加前缀、去掉前缀
查看>>
mysql 主从
查看>>
mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
查看>>
mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
查看>>
mysql 主从关系切换
查看>>
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>