原始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. 开个帖,开始学习shell编程

    恩,就这么开始了 首先说一下这个小小的插曲...写着写着,它就崩溃了,然后我也崩溃了...崩溃的我到处找一个不会让我崩溃的工具,然后找来找去就找到了scribefire. 1. 关于从Command ...

  2. Queue 先进先出队列的操作

    1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...

  3. iOS GCD中的dispatch_group

    假如有一组任务,A,B,C,D,其中ABC是可以并行的,D是必须在ABC任务完成后再执行的. (举个场景,比如吃饭前必须先做菜.做饭和买饮料,然后才能开吃) 1.关于ABC的并行: 采用多线程的方式就 ...

  4. Beta Daily Scrum 第六天

    [目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 昨天完成任务,今天看有没有bug,找一找 好像没什么事 615 ...

  5. Theano tutorial – basic type

    博客摘自:Deep learning 第二篇 婴儿学步 Theano如何做算数? import theano.tensor as T from theano import function x=T.d ...

  6. Android 使用 DownloadManager 管理系统下载任务的方法,android管理系统

    从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...

  7. 我所了解的JavaScript糟粕和鸡肋

    糟粕 全局变量 众所周知,全局变量在很小的程序中可能会带来方便,但随着程序变得越来大,全局变量将难以处理,全局变量将降低程序的可靠性. 在js中有3种方式定义全局变量 脱离任何函数安排一个var语句  ...

  8. POJ2185Milking Grid(最小覆盖子串 + 二维KMP)

    题意: 一个r*c的矩形,求一个子矩形通过平移复制能覆盖整个矩形 关于一个字符串的最小覆盖子串可以看这里http://blog.csdn.net/fjsd155/article/details/686 ...

  9. POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)

    题目链接 题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和 参考斌神的代码 #include <iostream> #include <cstring> # ...

  10. css013 构建基于浮动的布局

    css013 构建基于浮动的布局 基于浮动的布局时利用float属性是网页上的元素并排,并创建列 float有三个值:left .right .none 1.假设要把一张图片浮动到网页的左侧 .flo ...