drgawfsf1069 2016-10-12 04:57
浏览 177
已采纳

如何在golang mgo.v2库中使用MongoDB获得汇总管道结果

Question background is I want retrieve aggregated data from MongoDB data by using golang mgo.v2 liabary.

I have a collection dataset as following.collection name is useragents

{
    "_id" : ObjectId("57f940c4932a00aba387b0b0"),
    "tenantID" : 1,
    "date" : "2016-10-09 00:23:56",
    "venueList" : [
        {
            "id" : “VID1212”,
            "sum" : [
                {
                      "name" : "linux",
                      "value" : 12
                },
                {
                    "name" : "ubuntu",
                    "value" : 4
                }
            ],
            “ssidList” : [    // this is list of ssid’s in venue
                {
                    "id" : “SSID1212”,
                    "sum" : [
                        {
                            "name" : "linux",
                            "value" : 8
                        },
                        {
                            "name" : "ubuntu",
                            "value" : 6
                        }
                    ],
                    “macList” : [  // this is mac list inside particular ssid  ex: this is mac list inside the SSID1212
                        {
                            "id" : “12:12:12:12:12:12”,
                            "sum" : [
                                {
                                    "name" : "linux",
                                    "value" : 12
                                },
                                {
                                    "name" : "ubuntu",
                                    "value" : 1
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "id" : “VID4343”,
            "sum" : [
                {
                     "name" : "linux",
                     "value" : 2
                }
            ],
            "ssidList" : [
                {
                    "id" : “SSID4343”,
                    "sum" : [
                        {
                            "name" : "linux",
                            "value" : 2
                        }
                    ],
                    "macList" : [
                        {
                            "id" : “43:43:43:43:43:34”,
                            "sum" : [
                                {
                                    "name" : "linux",
                                    "value" : 2
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

I have already solved MongoDB shell script that i want by help from stack-overflow community.but I am troubling on when I was implemening it in golang using mgo.v2 library.

This is mongodb shell script

db.useragents.aggregate([
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
    { "$unwind": "$venueList" },
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
    { "$unwind": "$venueList.sum" },    
    { 
        "$group": {
            "_id": "$venueList.sum.name",
            "count": { "$sum": "$venueList.sum.value" }
        }
    },
    { 
        "$group": {
            "_id": null,
            "counts": {
                "$push": {
                    "name": "$_id",
                    "value": "$count"
                }
            }
        }
    }
])

Please go though question background

And I implement golang code as following

func GetBrowserStats(constrains models.Constrains) ([]bson.M, error) {

    session := commons.GetMongoSession()
    defer session.Close()
    var col = session.DB("analytics").C("useragents")

    pipeline1 := bson.M{
        "$match": bson.M{
            "venueList.id": bson.M{
                "$in": []string{"VID1212", "VID4343"},
            },
        },
    }
    pipeline2 := bson.M{
        "$unwind": "$venueList",
    }
    pipeline3 := bson.M{
        "$match": bson.M{
            "venueList.id": bson.M{
                "$in": []string{"VID1212", "VID4343"},
            },
        },
    }
    pipeline4 := bson.M{
        "$unwind": "$venueList.sum",
    }

    pipeline5 := bson.M{
        "$group": bson.M{
            "_id": "$venueList.sum.name",
            "count": bson.M{
                "$sum": "$venueList.sum.value",
            },
        },
    }
    pipeline6 := bson.M{
        "$group": bson.M{
            "_id": bson.NewObjectId(),
            "counts": bson.M{
                "$push": bson.M{
                    "name":  "$_id",
                    "value": "$count",
                },
            },
        },
    }

    all := []bson.M{pipeline1, pipeline2, pipeline3, pipeline4, pipeline5, pipeline6}
    pipe := col.Pipe(all)

    result := []bson.M{}
    err := pipe.All(&result)
    println(result[0])
    if err != nil {
        println(err.Error())
        errMsg := "Error occourred while getting dashboard configs from mongo stack:" + err.Error()
        log.Error()
        return result, errors.New(errMsg)
    }
    return result, nil
}

I have create a pipline and serve it into pipe.All() but there is null result return from the result varible.

I want to return following object in result

{ "_id" : ObjectId("57f73573d6e0ac1a9f2ab346") , "counts" : [ { "name" : "ubuntu", "value" : 1 }, { "name" : "linux", "value" : 14 } ] }
  • 写回答

1条回答 默认 最新

  • duan19913 2016-10-13 11:32
    关注

    Finally I find the solution.I like to share it with stackoverflow community.

    I refer this one : google group answer

     session := commons.GetMongoSession()
        defer session.Close()
    
        pipeline := []bson.D{
            bson.D{
                {"$match",
                    bson.M{
                        "venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}},
                    },
                },
            },
            bson.D{
                {"$unwind", "$venueList"},
            },
            bson.D{
                {"$match",
                    bson.M{
                        "venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}},
                    },
                },
            },
            bson.D{
                {"$unwind", "$venueList.sum"},
            },
            bson.D{
                {"$group",
    
                    bson.M{
                        "_id": "$venueList.sum.name",
                        "count": bson.M{
                            "$sum": "$venueList.sum.value",
                        },
                    },
                },
            },
            bson.D{
                {"$group",
                    bson.M{
                        "_id": bson.NewObjectId(),
                        "counts": bson.M{
                            "$push": bson.M{
                                "name":  "$_id",
                                "value": "$count",
                            },
                        },
                    },
                },
            },
        }
    
        query := bson.D{
            {"aggregate", "useragents"}, // useragents is a collection name
            {"pipeline", pipeline},
        }
    
        var res interface{}
        err := session.DB("analytics").Run(query, &res)
        if err != nil {
            println(err.Error())
        } else {
            println(res)
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 软件定义网络mininet和onos控制器问题
  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。