go语言mongdb管道使用(二)

原始代码:

/*
	重点项目实体机需求汇总 查询数据
 */
func (this *IndexController) ProjectReqTotalData() {


fmt.Println("ddddddd-------------------->hhhhhhhh--------------------------->")

	startTime := this.GetString("startTime")
	endTime := this.GetString("endTime")
	projectname := this.GetString("projectname")



	/*
db.process_list_info.aggregate(
{"$match":{"status":551}},
{"$unwind":"$detail.detail"},
{"$group":
	{"_id":{
		"peizhi":"$detail.detail.template",
		"projectname":"$detail.projectname_text.text",
		"project":"$detail.projectname",
		"service":"$detail.service",
		"servicename":"$detail.service_text.text"
		},
		"tatalNum":{"$sum":"$detail.detail.num_int"}}},
		{"$project":{"peizhi_id":"$_id.peizhi","netReq":"$_id.netReq","num":"$tatalNum"}})
 */


	/*
		国内部分
	 */
	matchConditionMap1 := map[string]interface{}{}
	matchConditionMap1["detail.purchase_type_text.id"] = "1"
	matchConditionMap1["code"] = "fwq_add"

	neMap1 := map[string]interface{}{}
	neMap1["status"] = int64(10)
	neMap2 := map[string]interface{}{}
	neMap2["status"] = int64(17)
	matchConditionMap1["$nor"] = [2]interface{}{neMap1,neMap2}

	//matchConditionMap1["status"] = int64(551)

	if  startTime != "" {
		q_map := map[string]interface{}{}
		q_map["$gte"] = startTime
		matchConditionMap1["createtime"] = q_map
	}

	if  endTime != "" {
		q_map2 := map[string]interface{}{}
		q_map2["$lte"] = endTime
		matchConditionMap1["createtime"] = q_map2
	}

	if  projectname != "" {
		matchConditionMap1["detail.projectname"] = projectname
	}

	matchMap := map[string]interface{}{}
	matchMap["$match"] = matchConditionMap1

	unwindMap := map[string]interface{}{}
	unwindMap["$unwind"] = "$detail.detail"




	//国内条件
	queryMap1 := map[string]interface{}{}
	queryMap2 := map[string]interface{}{}
	queryMap3 := map[string]interface{}{}
	queryMap4 := map[string]interface{}{}
	queryMap5 := map[string]interface{}{}

	queryMap1["detail.detail.netReq"] = "568a7d67b9687e31c81d8379"  //核心纯内网
	queryMap2["detail.detail.netReq"] = "568a7dadb9687e31c81d8395"  //电信公网IP
	queryMap3["detail.detail.netReq"] = "569f76952ae6ff1d0043ed6f"  //联通公网IP
	queryMap4["detail.detail.netReq"] = "569f82e62ae6ff1d0043ed72"  //多线公网IP
	queryMap5["detail.detail.netReq"] = "56c2f8102ae6ff1d004c2a8f"  //外地机房



	queryArray := []interface{}{}
	queryArray = append(queryArray,queryMap1)
	queryArray = append(queryArray,queryMap2)
	queryArray = append(queryArray,queryMap3)
	queryArray = append(queryArray,queryMap4)
	queryArray = append(queryArray,queryMap5)

	qMap := map[string]interface{}{}
	qMap["$or"] = queryArray

	matchMap2 := map[string]interface{}{}
	matchMap2["$match"] = qMap

	groupMap := map[string]interface{}{}
	idMap := map[string]interface{}{}
	idMap["peizhi"] = "$detail.detail.template"
	idMap["peizhiname"] = "$detail.detail.template_text.text"
	idMap["projectname"] = "$detail.projectname_text.text"
	idMap["project"] = "$detail.projectname"
	//idMap["service"] = "$detail.service"
	//idMap["servicename"] = "$detail.service_text.text"

	sumMap := map[string]interface{}{}
	sumMap["$sum"] = "$detail.detail.num"

	groupMap["_id"] = idMap
	groupMap["tatalNum"] = sumMap

	mapGroup := map[string]interface{}{}
	mapGroup["$group"] = groupMap

	sortMap := map[string]interface{}{}
	sort1:=map[string]interface{}{}
	//sort1["tatalNum"] = 1
	//sort1["_id"] = 1
	sort1["_id.projectname"] = 1
	//sort1["_id.service"] = 1
	sort1["_id.peizhi"] = 1
	sortMap["$sort"] = sort1

	groupArr := make([]interface{}, 0)
	groupArr = append(groupArr, matchMap)
	groupArr = append(groupArr, unwindMap)
	groupArr = append(groupArr, matchMap2)
	groupArr = append(groupArr, mapGroup)
	groupArr = append(groupArr, sortMap)


	//fmt.Println("打印参数>>国内——————————————>",groupArr)
	var baseMongo componets.BaseMongo
	resultIn := baseMongo.FindPipe("lingshu", "process_list_info", groupArr)

	//fmt.Println("打印结果>>国内------>")
	//fmt.Println(resultIn)





	/*
		国外部分
	 */
	matchConditionMap1_out := map[string]interface{}{}
	matchConditionMap1_out["detail.purchase_type_text.id"] = "1"
	matchConditionMap1_out["code"] = "fwq_add"
	//matchConditionMap1_out["status"] = int64(551)

	matchMap_out := map[string]interface{}{}
	matchMap_out["$match"] = matchConditionMap1_out

	unwindMap_out := map[string]interface{}{}
	unwindMap_out["$unwind"] = "$detail.detail"


	qMap_out := map[string]interface{}{}
	qMap_out["$nor"] = queryArray  // 国外条件
	//matchConditionMap2["$detail.detail.netReq"] = "not in"  //国内条件
	matchMap2_out := map[string]interface{}{}
	matchMap2_out["$match"] = qMap_out

	groupMap_out := map[string]interface{}{}
	idMap_out := map[string]interface{}{}
	idMap_out["peizhi"] = "$detail.detail.template"
	idMap_out["peizhiname"] = "$detail.detail.template_text.text"
	idMap_out["projectname"] = "$detail.projectname_text.text"
	idMap_out["project"] = "$detail.projectname"
	//idMap_out["service"] = "$detail.service"
	//idMap_out["servicename"] = "$detail.service_text.text"

	sumMap_out := map[string]interface{}{}
	sumMap_out["$sum"] = "$detail.detail.num"

	groupMap_out["_id"] = idMap_out
	groupMap_out["tatalNum_out"] = sumMap_out

	mapGroup_out := map[string]interface{}{}
	mapGroup_out["$group"] = groupMap_out

	sortMap_out := map[string]interface{}{}
	sort2:=map[string]interface{}{}
	//sort2["_id"] = 1
	sort2["_id.projectname"] = 1
	//sort2["_id.service"] = 1
	sort2["_id.detail.template"] = 1
	sortMap_out["$sort"] = sort2

	groupArr_out := make([]interface{}, 0)
	groupArr_out = append(groupArr_out, matchMap)
	groupArr_out = append(groupArr_out, unwindMap_out)
	groupArr_out = append(groupArr_out, matchMap2_out)
	groupArr_out = append(groupArr_out, mapGroup_out)
	groupArr_out = append(groupArr_out, sortMap_out)


	//fmt.Println("打印参数>>国外——————————————>",groupArr_out)
	var baseMongo_out componets.BaseMongo
	resultOut := baseMongo_out.FindPipe("lingshu", "process_list_info", groupArr_out)

	//fmt.Println("打印结果>>国外------>")
	//fmt.Println(resultOut)




	for _,v := range resultIn {

		inMap := v.(bson.M)
		v.(bson.M)["tatalNum_out"] = float64(0)
		groupid := inMap["_id"].(bson.M)
		for _, v_out := range resultOut {
			outMap := v_out.(bson.M)
			groupid_out := outMap["_id"].(bson.M)

			if groupid_out["peizhi"].(string) == groupid["peizhi"].(string) && groupid_out["projectname"].(string) == groupid["projectname"].(string) && groupid_out["project"].(string) == groupid["project"].(string){
				v.(bson.M)["tatalNum_out"] = outMap["tatalNum_out"]
				v_out.(bson.M)["is_merge"] = "1"
			}
		}

	}


	//fmt.Println("打印out处理后数据——————————>",resultOut)

	for _,m := range resultOut {
		if m.(bson.M)["is_merge"]==nil{

			m.(bson.M)["tatalNum"] = float64(0)
			resultIn = append(resultIn,m)

		}


	}

	utils_r := componets.Utils{}

	for _, item := range resultIn {
		template_id := item.(bson.M)["_id"].(bson.M)["peizhi"].(string)
		objectid := bson.ObjectIdHex(template_id)

		itemRes := baseMongo.FindById("lingshu", "cmdb_store_dict",objectid)

		fmt.Println("打印模版信息————————>",itemRes)
		templateName := itemRes.(bson.M)["templateName"]
		cost := itemRes.(bson.M)["cost"]
		price := "0"
		if cost!=nil {
			price = cost.(string)
			if price=="" {
				price ="0"
			}
		}

		inNum := float64(0)
		outNum := float64(0)

		if(reflect.TypeOf(item.(bson.M)["tatalNum"]).Name()=="int"){
			inNum = float64(item.(bson.M)["tatalNum"].(int))
		}else {
			inNum = item.(bson.M)["tatalNum"].(float64)
		}


		if(reflect.TypeOf(item.(bson.M)["tatalNum_out"]).Name()=="int"){
			outNum = float64(item.(bson.M)["tatalNum_out"].(int))
		}else {
			outNum = item.(bson.M)["tatalNum_out"].(float64)
		}



		tempprice,_ := strconv.ParseFloat(price,64)
		price_float := utils_r.Round(tempprice,2)

		inTotalMoney := price_float * inNum
		outTotalMoney := price_float * outNum

		item.(bson.M)["inTotalMoney"] = utils_r.Round(inTotalMoney,2)
		item.(bson.M)["outTotalMoney"] = utils_r.Round(outTotalMoney,2)

		item.(bson.M)["_id"].(bson.M)["jixingName"] = templateName
		item.(bson.M)["cost"] = price_float
	}

	//fmt.Println("发送组合结果-——————————>",resultIn)


	this.Data["resultIn"] = resultIn



	this.Data["Baseurl"]=beego.AppConfig.String("baseurl")

	this.TplNames = "statistics/projectreqtotaldata.html"
}

mongdb原始命令:

b.process_list_info.aggregate(
{"$match":{"status":551}},
{"$unwind":"$detail.detail"},
{"$group":
	{"_id":{
		"peizhi":"$detail.detail.template",
		"projectname":"$detail.projectname_text.text",
		"project":"$detail.projectname",
		"service":"$detail.service",
		"servicename":"$detail.service_text.text"
		},
		"tatalNum":{"$sum":"$detail.detail.num_int"}}},
		{"$project":{"peizhi_id":"$_id.peizhi","netReq":"$_id.netReq","num":"$tatalNum"}})





db.process_list_info.aggregate({"$unwind":"$detail.detail”})

db.process_list_info.aggregate({"$match":{"detail.projectname_text.id":"575789b9f0a2d7e179ac750b"}},{"$unwind":"$detail.detail”})

db.process_list_info.aggregate({"$match":{"detail.projectname_text.id":"575789b9f0a2d7e179ac750b"}},{"$unwind":"$detail.detail"},{"$project":{"tttt":"$createtime"}})


db.process_list_info.aggregate({"$match":{"detail.projectname_text.id":"575789b9f0a2d7e179ac750b","orderid":8285}},{"$unwind":"$detail.detail"},{"$group":{"_id":"$detail.detail.template","tatalNum":{"$sum":"$detail.detail.num"}}},{"$project":{"peizhi_id":"$_id","num":"$tatalNum"}})


db.process_list_info.aggregate({"$match":{"status":551}},{"$unwind":"$detail.detail"},{"$group":{"_id":{"peizhi":"$detail.detail.template","netReq":"$detail.detail.netReq"},"tatalNum":{"$sum":"$detail.detail.num_int"}}},{"$project":{"peizhi_id":"$_id.peizhi","netReq":"$_id.netReq","num":"$tatalNum"}})



 db.process_list_info.aggregate({"$match":{"status":551}},{"$unwind":"$detail.detail"},{"$group":{"_id":{"peizhi":"$detail.detail.template","projectname":"$detail.projectname_text.text","project":"$detail.projectname","service":"$detail.service","servicename":"$detail.service_text.text"},"tatalNum":{"$sum":"$detail.detail.num_int"}}},{"$project":{"peizhi_id":"$_id.peizhi","netReq":"$_id.netReq","num":"$tatalNum"}})
注意:不管是mongdb原始命令,还是go调用,$sum操作符号,只能汇总数值类型的字段(比如,Double,float等);之前保存的时候num字段保存为string类型(“5”),无法汇总;


db.process_list_info.find({"code":"fwq_add"}).forEach(function(obj){obj.detail.detail.forEach(function(bj){ bj.num = parseFloat(bj.num) } );db.process_list_info.save(obj)  })

db.process_list_info.find({"code":"fwq_mv_add"}).forEach(function(obj){obj.detail.detail.forEach(function(bj){ bj.num = parseFloat(bj.num) } );db.process_list_info.save(obj)  })

这样处理以后num字段转换成了Double;

 

db.foo.find({bad: {$exists: true}}).forEach(function(obj) { 
obj.user_id = new NumberInt(obj.user_id);
db.foo.save(obj);
});

go的管道调用

func (this *BaseMongo) FindPipe(dataBase string, col string, queryStruct []interface{}) []interface{} {
	if session.Ping() != nil {
		doConn()
	}
	c := session.DB(dataBase).C(col)
	var result []interface{}
	c.Pipe(&queryStruct).All(&result)
	return result
}

原文发布于微信公众号 - Golang语言社区(Golangweb)

原文发表时间:2016-06-29

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏大数据和云计算技术

算法基础7:平衡查找树概述

算法是基础,小蓝同学准备些总结一系列算法分享给大家,这是第7篇《平衡查找树概述》,非常赞!希望对大家有帮助,大家会喜欢! 前面系列文章: 归并排序 #算法...

3389
来自专栏Petrichor的专栏

leetcode: 75. Sort Colors

1543
来自专栏算法与数据结构

PTA 7-4 排座位(25 分)

7-4 排座位(25 分) 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位。无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对...

2409
来自专栏小狼的世界

Aptana的破解

最近写JS比较多,常常苦恼与没有一个顺手的IDE。Editplus虽然用的熟,不过那个的效率太低而且代码看起来也很不方便,经过一个多月的试用,发现了一款好用的编...

1012
来自专栏木子昭的博客

明星程序员被Google挂掉的故事

首先要提一个软件Homebrew Homebrew可能是Mac上最好用的包管理器, 地位相当于Ubuntu的apt, 也相当于命令行版的AppStore ? ...

3755
来自专栏别先生

使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。

1、使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。

3202
来自专栏机器之心

资源 | 从算法到数据结构,百道面试问题实现答案集合

选自GitHub 作者:Sherali Obidov 机器之心编译 参与:李亚洲、微胖、蒋思源 该资源是算法、数据结构以及面试问题解决方案的集合,里面的 rep...

2816
来自专栏灯塔大数据

每周学点大数据 | No.26外存数据结构——B 树

No.26期 外存数据结构——B 树 小可:看来在磁盘上二叉搜索树对新来的数据插入树中支持得不好,那么究竟怎么解决这个问题呢? Mr. 王:有一个非常经典的...

3527
来自专栏前端迷

数据结构与前端开发(四)-树

二叉树拥有一个根节点,每个节点至多拥有两个子节点,分别为:左节点和右节点。树的最底部节点称之为叶节点,当一颗树的叶数量数量为满时,该树可以称之为满二叉树。

1983
来自专栏高性能服务器开发

算法导论第十三章 红黑树(2)

这一章我觉得难点就在于删除修复,插入修复是比较容易想到的,然后我认为需要着重注意的地方都记录下来了,下面贴上自己写的基于C++模板的代码,有点长。...

912

扫码关注云+社区

领取腾讯云代金券