原始json:

{"listsn": "","code": "fwq_add","detail": {"appdate": "2016-06-28","expectdate": "2016-06-30","service_text": {"text": "NAT管理(乐视云计算有限公司产品研发中心_基础架构_网络服务_NAT管理)","id": "343"},"projectname_text": {"id": "575789b9f0a2d7e179ac750b","text": "采购项目"},"note": "test","totalmoney": 132500,"detail": [{"buss": "","alamLinks_text": [{"text": "张三1","id": "zhangsan1@qq.com"}],"opSystem_text": {"id": "CentOS 6.6","text": "CentOS 6.6"},"buss_text": {"id": "","text": ""},"template": "569f89b02ae6ff1d0043ed7b","use_type_text": {"id": "1","text": "生产环境"},"fpData": [],"follow_idc_text": {"id": "1","text": "否"},"alamLinks": "zhangsan1@qq.com","num": 2,"modular": "test","follow_idc": "1","netReq_text": {"text": "美国机房","id": "569f9ae72ae6ff1d0043edc1"},"opSystem": "CentOS 6.6","netReq": "569f9ae72ae6ff1d0043edc1","use_type": "1","template_text": {"id": "569f89b02ae6ff1d0043ed7b","text": "A1-1  [CPU:24C(E5-2620V3*2)          内存:128G(16G*8)          硬盘:600G(系统 raid1)          ||标配实体机(普通应用)||26500 ]"}},{"buss_text": {"id": "","text": ""},"num": 3,"use_type": "2","follow_idc_text": {"id": "1","text": "否"},"netReq": "568a7d67b9687e31c81d8379","opSystem_text": {"id": "CentOS 6.6","text": "CentOS 6.6"},"modular": "test2","use_type_text": {"text": "测试环境","id": "2"},"template": "569f89c32ae6ff1d0043ed7c","alamLinks_text": [{"id": "zhangsan1@letv.com","text": "张三1"}],"follow_idc": "1","alamLinks": "zhangsan1@letv.com","opSystem": "CentOS 6.6","buss": "","template_text": {"id": "569f89c32ae6ff1d0043ed7c","text": "A1-2  [CPU:24C(E5-2620V3*2)          内存:128G(16G*8)          硬盘:600G(系统 raid1),3T(数据 raid5)          ||标配实体机(普通应用)||26500 ]"},"netReq_text": {"id": "568a7d67b9687e31c81d8379","text": "核心纯内网"},"fpData": []}],"marate": "必要性说明test","projectname": "575789b9f0a2d7e179ac750b","service": "343","explain": "必要性说明test","purchase_type": "1","purchase_type_text": {"id": "1","text": "重点项目"}},"desc": "测试单据","reader": "","createtime": "2016-06-28 09:54:15","type": "","uuid": "cecaf698-a2d7-41fa-94b1-d157f38b3cfc","status": 593,"updatetime": "2016-06-28 09:54:15","orderid": 7437,"title": "测试单据","owner": "zhangsan1@qq.com","qq": "187287822","leader": "李四 - ","owner_name": "zhangsan1(zhangsan1@qq.com)","telphone": "13718899792"
}

  

原始代码:

/*重点项目实体机需求汇总 查询数据*/
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"] = startTimematchConditionMap1["createtime"] = q_map}if  endTime != "" {q_map2 := map[string]interface{}{}q_map2["$lte"] = endTimematchConditionMap1["createtime"] = q_map2}if  projectname != "" {matchConditionMap1["detail.projectname"] = projectname}matchMap := map[string]interface{}{}matchMap["$match"] = matchConditionMap1unwindMap := 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"  //电信公网IPqueryMap3["detail.detail.netReq"] = "569f76952ae6ff1d0043ed6f"  //联通公网IPqueryMap4["detail.detail.netReq"] = "569f82e62ae6ff1d0043ed72"  //多线公网IPqueryMap5["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"] = queryArraymatchMap2 := map[string]interface{}{}matchMap2["$match"] = qMapgroupMap := 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"] = idMapgroupMap["tatalNum"] = sumMapmapGroup := map[string]interface{}{}mapGroup["$group"] = groupMapsortMap := map[string]interface{}{}sort1:=map[string]interface{}{}//sort1["tatalNum"] = 1//sort1["_id"] = 1sort1["_id.projectname"] = 1//sort1["_id.service"] = 1sort1["_id.peizhi"] = 1sortMap["$sort"] = sort1groupArr := 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.BaseMongoresultIn := 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_outunwindMap_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_outgroupMap_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_outgroupMap_out["tatalNum_out"] = sumMap_outmapGroup_out := map[string]interface{}{}mapGroup_out["$group"] = groupMap_outsortMap_out := map[string]interface{}{}sort2:=map[string]interface{}{}//sort2["_id"] = 1sort2["_id.projectname"] = 1//sort2["_id.service"] = 1sort2["_id.detail.template"] = 1sortMap_out["$sort"] = sort2groupArr_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.BaseMongoresultOut := 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 * inNumoutTotalMoney := price_float * outNumitem.(bson.M)["inTotalMoney"] = utils_r.Round(inTotalMoney,2)item.(bson.M)["outTotalMoney"] = utils_r.Round(outTotalMoney,2)item.(bson.M)["_id"].(bson.M)["jixingName"] = templateNameitem.(bson.M)["cost"] = price_float}//fmt.Println("发送组合结果-——————————>",resultIn)this.Data["resultIn"] = resultInthis.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
}

  

  

转载于:https://www.cnblogs.com/8899man/p/5622443.html

go语言mongdb管道使用相关推荐

  1. Go语言的管道Channel用法

    本文实例讲述了Go语言的管道Channel用法.分享给大家供大家参考.具体分析如下: channel 是有类型的管道,可以用 channel 操作符 <- 对其发送或者接收值. ch <- ...

  2. C 语言编程 — 管道(Pipe)

    目录 文章目录 目录 Linux 的管道指令 C 语言的匿名管道 命名管道 匿名管道和命名管道的区别 参考文档 Linux 的管道指令 如果你熟悉 Linux,那么你对管道应该不会感到陌生,我们经常会 ...

  3. R语言中管道操作 %%, %T%, %$% 和 %%

    前言 使用R语言进行数据处理是非常方便的,几行代码就可以完成很复杂的操作.但是,对于数据的连续处理,还是有人觉得代码不好看,要么是长长的函数嵌套调用,有点像Lisp感觉,括号包一切:要么就是每次操作赋 ...

  4. magrittr | R语言的管道操作符

    管道操作符(Pipe Operator)是一个特定的符号,它可以将前一行代码的输出传递给后一行代码作为输入,从而将原本相互独立的两行代码连接在一起.而通过不断地使用管道操作符,最终可以将多行代码写成& ...

  5. c语言父子进程,C语言无名管道实现父子进程间通信

    /** *父子进程中管道通信 * */ #include #include #include #include #include #include #define SIZE 128//缓存区大小 ch ...

  6. 用c语言如何实现管道的大小,C语言编写管道代码

    什么是管道: 管道是Unix中最古老的进程间通信的形式. 我们把从一个进程链接到另外一个进程的一个数据流称为管道. 本质是有固定大小的内核缓存区. 管道的限制 管道是半双工的,数据只能向一个方向流动, ...

  7. Go语言并发编程简介

    并发的基础知识 进程与线程的回顾总结: 进程的定义: 进程比较通用的几个定义: 进程是程序的一次执行过程 进程是一个程序及其数据在处理机上顺序执行时所发生的活动 进程是具有独立功能的程序在一个数据集合 ...

  8. 和自然语言处理有关的英语_自然语言处理对非英语语言的重要性

    和自然语言处理有关的英语 Natural Language Processing (NLP) is growing in use and plays a vital role in many syst ...

  9. Elasticsearch:在 Elasticsearch 中使用语言识别进行多语言搜索

    我们很高兴地宣布,随着机器学习推理摄入处理器 (inference ingest processor)的发布,我们还将在 Elasticsearch 7.6 中发布语言识别. 在此发行版中,我们希望借 ...

  10. R语言爬虫:当当图书畅销榜(近7日)

    时间:2018-10-9 爬取内容:当当图书畅销榜(近7日):书名.作者.出版社.推荐率.出版时间.价格.折扣.网址 R语言爬虫:当当图书畅销榜(近7日) 通过对其他人爬虫代码的学习,发现有相当一部分 ...

最新文章

  1. Visual Studio中断NPM套件库服务
  2. java 连连看_java—连连看-实现封装
  3. B树,B-树和B+树、B*树的区别
  4. Qt学习之路(4):初探信号槽
  5. 一文快速理解Java反射(详细对比图)
  6. ABP 集成 nswag 根据 Swagger API 自动生成操作类代码
  7. centos rm -rf 恢复删除的文件
  8. 网络拓扑结构_全球网络拓扑测绘这样来实现
  9. (内附教程链接及模板下载)如何利用ICEM进行内流道的抽取
  10. 风险评估在公路安全生命防护工程中的应用
  11. 马云的电影,丁磊的饭局
  12. 华东院进入华为智慧战略 其他院怎么办
  13. netkeeper不能建立远程计算机连接,Netkeeper客户端常见问题集锦.doc
  14. 指向性麦克风--心形麦克风设计(五)
  15. 如何启动一个vue项目
  16. 2020 Leetcode 春季赛战队赛 题目汇总
  17. 中级软考-软件设计师(四)
  18. gerrit 评审 报错“Submit including parents”
  19. CentOS 7修改ip地址(详细)
  20. 生态环境数据下载网站

热门文章

  1. linux .o,.a,.so文件区别是什么
  2. 【Django 2021年最新版教程28】前端模板中 时间日期星期格式化
  3. Ubuntu su root提示输入密码 怎么输入都不对
  4. requests.exceptions.ConnectionError: (‘Connection aborted.’, RemoteDisconnected(‘Remote end closed c
  5. linux mysql 数据库同步
  6. SpringMVC学习(八)Ajax异步请求
  7. css叠层_CSS 理解样式层叠
  8. thinkphp5之配置tp5重写伪静态
  9. MySQL学习(一、概述和表的基本操作)
  10. springboot 微服务相关收藏