关闭

mongodb的高级操作(聚合框架)

标签: mongodb sum
1694人阅读 评论(1) 收藏 举报
分类:

重点探讨mongodb  对某个字段 sum求和

new BasicDBObject("$group",new BasicDBObject("_id","$number").append("total", new BasicDBObject("$sum","$"+"length")));

new BasicDBObject("_id","$"+queryField)为固定格式


group by 查询 不要用java驱动带的group by ,要用2.2版本后的aggregate聚合框架来搞,经过试验速度快一倍

参考

官网:http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
实例:http://www.yeetrack.com/?p=649


观看例子前,请先看上面对的2个参考文档,特别是官网的


例子 

		// 首先利$match筛选出where条件
		BasicDBObject[] array = {
				new BasicDBObject("startTimeLong", new BasicDBObject("$gte",beginTime)),
				new BasicDBObject("startTimeLong", new BasicDBObject("$lt",endTime)) };

		BasicDBObject cond = new BasicDBObject();
		cond.put("$and", array);
		DBObject match = new BasicDBObject("$match", cond);
		
		// 利用$project拼装group需要的数据,包含optCode列、processTime列
		DBObject fields = new BasicDBObject("optCode", 1); // 接口
		fields.put("processTime", 1);// 耗时
		fields.put("provinceCode", 1);// 省份
		fields.put("channelSubCode", 1);// 渠道
		fields.put("platFormCode", 1);// 平台

		DBObject project = new BasicDBObject("$project", fields);

		// 利用$group进行分组
		DBObject _group = new BasicDBObject("provinceCode", "$provinceCode");
		_group.put("channelSubCode", "$channelSubCode");
		_group.put("platFormCode", "$platFormCode");
		_group.put("optCode", "$optCode");

		DBObject groupFields = new BasicDBObject("_id", _group);                  
		//总数
    		groupFields.put("count", new BasicDBObject("$sum", 1));
                //求和		
		groupFields.put("processTime_sum", new BasicDBObject("$sum","$processTime"));
                DBObject group = new BasicDBObject("$group", groupFields);
		AggregationOutput output = MongodbUtil.getLogInfoCollection().aggregate(match, project, group);



/**
	* 对一个字段求和
	* @param collectionName
	* @param where
	* @param groupFields
	* @return
	*/
	public Double sum(String collectionName,DBObject where,String sumField){
		
		DBCollection col=mongoDbFactory.getDb().getCollection(collectionName);
		DBObject match=new BasicDBObject("$match",where);
		DBObject group=new BasicDBObject("$group",new BasicDBObject("_id",null).append("total", new BasicDBObject("$sum","$"+sumField)));
		List<DBObject> list=(List<DBObject>)col.aggregate(match,group).results();
		if(list.size()>0){
			return Double.valueOf(list.get(0).get("total").toString());
		}
		return 0D;
	}




个人实例:查询消费总和

public long getCostCount(String number, String type, int month) {
		DBCollection dbc = getCollection(Constant.MongoDBName, Constant.CostDetails);
		DBObject dbObject = new BasicDBObject();
		dbObject.put("number", number);
		dbObject.put("type", type);
		dbObject.put("month", month);
		
		//设置Where条件  
	    DBObject match = new BasicDBObject("$match",dbObject);
	    //设置分组字段  
		DBObject group=new BasicDBObject("$group",new BasicDBObject("_id","$number").append("total", new BasicDBObject("$sum","$"+"length")));
		List<DBObject> list=(List<DBObject>)dbc.aggregate(match,group).results();
		if(list != null && list.size()>0){
			LOG.info(list.get(0).toString());
			return Long.valueOf(list.get(0).get("total").toString());
		}else{
			return 0l;
		}
	}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:675823次
    • 积分:9476
    • 等级:
    • 排名:第1897名
    • 原创:259篇
    • 转载:240篇
    • 译文:2篇
    • 评论:35条
    最新评论