mongodb - 如何在 golang mgo.v2 库中使用 MongoDB 获取聚合管道结果

标签 mongodb go aggregation-framework bson mgo

问题背景是我想使用 golang mgo.v2 liabary 从 MongoDB 数据中检索聚合数据。

我有一个集合数据集如下。集合名称是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
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

在 stack-overflow 社区的帮助下,我已经解决了我想要的 MongoDB shell 脚本。但是当我使用 mgo.v2 库在 golang 中实现它时,我遇到了麻烦。

这是 mongodb shell 脚本

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

请通过question background

我实现的golang代码如下

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
}

我已经创建了一个管道并将其提供给 pipe.All() 但是 result 变量返回的结果为空。

我想在 result 中返回以下对象

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

最佳答案

我终于找到了解决方案。我想与 stackoverflow 社区分享它。

我推荐这个: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)
    }

关于mongodb - 如何在 golang mgo.v2 库中使用 MongoDB 获取聚合管道结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39990793/

相关文章:

arrays - 查找 MongoDB 中数组字段包含某些子集的文档

paypal - 去 Paypal REST API请求

node.js - MongoDB 聚合和组嵌套字段

arrays - Go 中的数组赋值 : Content copy or Content pointer copy

go - 在 golang 中,如何覆盖嵌入式结构的方法

MongoDB 聚合查询相当于 PostsgreSQL

mongodb - Mongodb - 过滤嵌套数组中的数据元素

MONGODB 无法与 DB Mongo::Error::SocketError 握手:EOFError:到达文件结尾(TLS)

python - 如何使用 MongoEngine 创建 ArrayField?

node.js - mongoose.js 中的架构和子文档