原始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"] = 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
}

  

  

go语言mongdb管道使用的更多相关文章

  1. Go语言的管道Channel用法

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

  2. C语言与管道

    int main() { int s; int n; float avg; scanf("%d,%d",&s,&n); //特别注意的地方 // scanf(&qu ...

  3. Go语言 channel 管道 阻塞 死锁 经典问题

    建议阅读:14.2协程间的信道 问题:为什么代码1会报死锁的错误,而代码2不会报错? 代码1: package main import ( "fmt" ) func main() ...

  4. Nil Channels Always Block(Go语言中空管道总是阻塞)

    译自:https://www.godesignpatterns.com/2014/05/nil-channels-always-block.html 原作者:Alex Lockwood 在本篇文章中, ...

  5. Go语言中的管道(Channel)总结

    管道(Channel)是Go语言中比较重要的部分,经常在Go中的并发中使用.今天尝试对Go语言的管道来做以下总结.总结的形式采用问答式的方法,让答案更有目的性. Q1.管道是什么? 管道是Go语言在语 ...

  6. Go并发模式:管道与取消

    关键字:Go语言,管道,取消机制,并发,sync.WaitGroup,包引用,通道,defer,select GO并发模式:管道与取消 简介 Go的并发能力可以使构建一个流数据管道变得非常容易,并且可 ...

  7. 你好,Go语言

    本文是「vangoleo的Go语言学习笔记」系列文章之一. 官网: http://www.vangoleo.com/go/hello-golang/ 我在2015年第一次接触Go语言,完成了Hello ...

  8. 《深入理解Spark:核心思想与源码分析》(第2章)

    <深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...

  9. Spark设计理念与基本架构

    1.基本概念 Spark中的一些概念: RDD(resillient distributed dataset):弹性分布式数据集. Partition:数据分区.即一个RDD的数据可以划分为多少个分区 ...

随机推荐

  1. JavaScript Ajax之美~

    JavaScript Ajax之美~ 曾经有一段时期,因为开发人员对JavaScript的滥用导致其遭受了一段时间的冷门时期,不被大家看好,后来,到了2005年,Google公司的很多技术都是用了aj ...

  2. Docker入门教程(五)Docker安全

    Docker入门教程(五)Docker安全 [编者的话]DockOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第五篇,介绍了Docker的安全问题,依然是老话重谈,入门者可以通 ...

  3. JS-百钱买百鸡案例-for循环制作

    <html> <head> <meta charset="utf-8"/> <title></title> <sc ...

  4. C#调用WebService实现天气预报

    http://zhangkui.blog.51cto.com/1796259/497324/ 本文使用Winform (C#)调用互联网上公开的WebServices(http://www.webxm ...

  5. O(1)快速乘注意事项

    O(1)快速乘是经典玄学优化啦~由于刚挂了一次特此总结一番. ll mul(ll u,ll v){ return(u*v-ll((long double)u*v/p)*p+p)%p; } double ...

  6. 【原】React中,map出来的元素添加事件无法使用

    在使用react中,经常用到react的map函数,用法和jquery里中的map一样,但是,如果你在每个map出来的元素中添加,你会发觉添加的事件无法关联, 比如,我们很多的评论,我需要在每个评论下 ...

  7. MySQL安装图解

    MySQL安装图解 打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行“setup.exe”,出现如下界面:           mysql安装向导启动,按“N ...

  8. WinForm------分页控件dll下载地址

    转载: http://files.cnblogs.com/wuhuacong/TestPager_SqlLite.rar

  9. sqlserver插入时发生在“xxx”处关键发生错误

    今天知道了一个小技巧,当你的数据库表名为user时会sqlserver的表发生冲突,所以因该将user这样用[user],ok 一切搞定 .

  10. PyCharm 教程(四)显示行号

    PyCharm 教程(四)显示行号 在PyCharm 里,显示行号有两种办法: 1,临时设置.右键单击行号处,选择 Show Line Numbers. 但是这种方法,只对一个文件有效,并且,重启Py ...