Go Language Mongdb Pipeline use

Source: Internet
Author: User

Raw JSON:

{"LISTSN": "", "Code": "Fwq_add", "detail": {"appdate": "2016-06-28", "expectdate": "2016-06-30", "service_t Ext ": {" text ":" Nat Management (Le Vision Cloud Computing Limited Product Development Center _ Infrastructure _ Network Service _nat Management) "," id ":" 343 "}," Projectname_text ": {" id ":        "575789b9f0a2d7e179ac750b", "Text": "Purchase Item"}, "note": "Test", "Totalmoney": 132500, "detail": [{           "Buss": "", "Alamlinks_text": [{"Text": "31", "id": "[email protected]"        }], "Opsystem_text": {"id": "CentOS 6.6", "text": "CentOS 6.6"}, "Buss_text": {"id": "", "Text": ""}, "template": "569f89b02ae6ff1d0043ed7b", "u          Se_type_text ": {" id ":" 1 "," Text ":" Production Environment "}," Fpdata ": []," Follow_idc_text ": { "id": "1", "text": "No"}, "Alamlinks": "[email protected]", "num": 2, "mo Dular ":" Test ","FOLLOW_IDC ":" 1 "," Netreq_text ": {" text ":" American Room "," id ":" 569F9AE72AE6FF1D0043EDC1 "},  "Opsystem": "CentOS 6.6", "Netreq": "569F9AE72AE6FF1D0043EDC1", "Use_type": "1", "Template_text":          {"id": "569f89b02ae6ff1d0043ed7b", "text": "A1-1 [cpu:24c (e5-2620v3*2) Memory: 128G (16G*8) HDD: 600G (System RAID1) | | Standard solid Machine (general application) | |        26500] "}}, {" Buss_text ": {" id ":" "," Text ":" "}," num ": 3, "Use_type": "2", "Follow_idc_text": {"id": "1", "text": "No"}, "Netreq": "56        8a7d67b9687e31c81d8379 "," Opsystem_text ": {" id ":" CentOS 6.6 "," text ":" CentOS 6.6 "}, "Modular": "Test2", "Use_type_text": {"text": "Test Environment", "id": "2"}, "template" : "569f89c32ae6ff1d0043ed7c", "Alamlinks_text": [{"id": "[Email proTected] "," text ":" 31 "}]," FOLLOW_IDC ":" 1 "," alamlinks ":" [Email protect Ed] "," Opsystem ":" CentOS 6.6 "," Buss ":" "," Template_text ": {" id ":" 569f89c32ae6ff1d0043ed          7c "," text ":" A1-2 [cpu:24c (e5-2620v3*2) Memory: 128G (16g*8) HDD: 600G (System RAID1), 3T (data RAID5) || Standard solid Machine (general application) | |        26500] "}," Netreq_text ": {" id ":" 568a7d67b9687e31c81d8379 "," text ":" Core Pure Intranet "}, "Fpdata": []}], "marate": "Necessity Description Test", "ProjectName": "575789b9f0a2d7e179ac750b", "Service": "343   "," explain ":" Necessity Description Test "," Purchase_type ":" 1 "," Purchase_type_text ": {" id ":" 1 "," text ":" Key Item "} }, "desc": "Test Document", "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 ":" Test Document "," owner ":" [email  pRotected] "," QQ ":" 187287822 "," leader ":" John Doe-"," owner_name ":" Zhangsan1 ([email protected]) "," Telphone ":" 1371 8899792 "}

  

Original code:

/* Key Project Entity requirements Rollup Query data */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 "}}) *//* domestic part */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 (+) 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]in terface{}{}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"//domestic conditions 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 "//Core Pure Intranet querymap2[" Detail.detail.netReq "] =" 568a7dadb9687e31c81d8395 "//Telecom public network ipquerymap3[ "Detail.detail.netReq"] = "569f76952ae6ff1d0043ed6f"//Unicom public network ipquerymap4["DETAIL.DETAIL.NETREQ"] = "569f82e62ae6ff1D0043ED72 "//Multi-line public network ipquerymap5[" DETAIL.DETAIL.NETREQ "] =" 56c2f8102ae6ff1d004c2a8f "//Field room 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 ("Printing parameters >> Domestic —————————————— >", Grouparr) var Basemongo componets. Basemongoresultin: = Basemongo.findpipe ("Lingshu", "Process_list_info", Grouparr)//fmt. Println ("Print results >> domestic------>")//fmt. PRINTLN (resultin)/* Foreign part */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//Foreign conditions//matchconditionmap2["$ Detail.detail.netReq "] =" not in "//Domestic condition 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 ("Printing parameters >> Foreign —————————————— >", grouparr_out) var basemongo_out componets. Basemongoresultout: = Basemongo_out. Findpipe ("Lingshu", "Process_list_info", Grouparr_out)//fmt. Println ("Print results >> foreign------>")//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 ("Print out post processing data —————————— >", 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 ("Print template information ———————— >", 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 ("Send combination result-—————————— >", Resultin) this. data["Resultin"] = resultinthis.data["Baseurl"]=beego. Appconfig.string ("BaseURL") this. Tplnames = "Statistics/projectreqtotaldata.html"}

Mongdb Original command:

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"}})




 

  

Note: Whether the mongdb original command, or go call, $sum operation symbol, can only summarize fields of numeric type (for example, double,float, etc.), the NUM field is saved as String type ("5"), and cannot be summarized; Db.process_ List_info.find ({"Code": "Fwq_add"}). ForEach (function (obj) {Obj.detail.detail.forEach (function (BJ) {bj.num = Parsefloat (Bj.num)});d B.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)});d B.process_list_ Info.save (obj)  }) After this processing, the NUM field is converted to double, db.foo.find ({bad: {$exists: true}}). ForEach (function (obj) {obj.user_id = new Numberint (obj.user_id);d b.foo.save (obj);});

  

Pipeline invocation of 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 Language Mongdb Pipeline use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.