go语言mongdb管道使用

原始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
}

  

  

 

posted @ 2016-06-28 10:03  8899man  阅读(306)  评论(0编辑  收藏  举报