博客
关于我
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 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>