mongodb 聚合在 golang 中给出错误

标签 mongodb go aggregation-framework mgo

我想要分组方法类型并根据它们的类型对它们进行计数。这些方法是字符串。我写了下面的代码。但是它给出了一个错误。

pipeline := []bson.D{
        bson.D{
            {"$unwind", "$method"},
        },
        bson.D{
            {"$group", bson.M{"_id": "$method", "count": bson.M{"$sum": 1}}},
        },
query := bson.D{
        {"aggregate", "API_ACCESS_LOGS"}, // useragents is a collection name
        {"pipeline", pipeline},
    }
  err = session.DB("vamps-logs").Run(query, &methods)

它给出了以下错误。

2018/02/06 09:58:33 http:紧急服务 127.0.0.1:53973:运行时错误:索引超出范围 goroutine 92 [正在运行]:

请帮我改正

最佳答案

您好,这是我修改后的示例代码,以便将管道与 MgO golang 一起使用。观看它并尝试应用它。 包主

import ("fmt"
    "log"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Method struct {
        Type string `json:"Type" bson:"Type"`
        Host float64 `json:"Host" bson:"Host"`
}

type Out struct {
        Type string `json:"Type" bson:"Type"`
        Count float64 `json:"count" bson:"count"`
}

func main() {

        session, err := mgo.Dial("localhost")
        if err != nil {
                panic(err)
        }
        defer session.Close()

        // Optional. Switch the session to a monotonic behavior.
        session.SetMode(mgo.Monotonic, true)

        c := session.DB("test").C("api")
        // err = c.Insert(&Method{"GET", 1.0},
           //         &Method{"PUT", 2.0},
           //         &Method{"POST", 2.0})
        if err != nil {
                log.Fatal(err)
        }

        pipe := c.Pipe(
                []bson.M{
                    bson.M{
                        "$unwind":"$Type",
                    },
                    bson.M{
                        "$group":bson.M{
                        "_id":"$Type", 
                        "count":bson.M{"$sum":1,},
                        },
                    },
                    bson.M{
                        "$project":bson.M{
                        "Type":"$_id" , "count":"$count",
                        },
                    },
                },
            )

        result := []Out{}
        // err = c.Find(bson.M{"Type": "GET" }).One(&result)
            err = pipe.All(&result)

        if err != nil {
                log.Fatal(err)
        }

        fmt.Println("Type:", result)
}

mongoDB 中的数据集

/* 1 */
{
    "_id" : ObjectId("5a794efd49d755038cc60307"),
    "Type" : "GET",
    "Host" : 1.0
}

/* 2 */
{
    "_id" : ObjectId("5a794efd49d755038cc60308"),
    "Type" : "PUT",
    "Host" : 2.0
}

/* 3 */
{
    "_id" : ObjectId("5a794efd49d755038cc60309"),
    "Type" : "POST",
    "Host" : 2.0
}

/* 4 */
{
    "_id" : ObjectId("5a794f3149d755038cc6031a"),
    "Type" : "GET",
    "Host" : 1.0
}

/* 5 */
{
    "_id" : ObjectId("5a794f3149d755038cc6031b"),
    "Type" : "PUT",
    "Host" : 2.0
}

/* 6 */
{
    "_id" : ObjectId("5a794f3149d755038cc6031c"),
    "Type" : "POST",
    "Host" : 2.0
}

输出

类型:[{POST 2} {PUT 2} {GET 2}]

关于mongodb 聚合在 golang 中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48635604/

相关文章:

mongodb - 将 db.currentOp 中的数据打印到网页

regex - 如何匹配不包含特定字符串的字符串

mongodb - 在 mongodb 中按带时间戳的日期聚合数据

mongodb - 使用 $slice 运算符获取数组的最后一个元素

python - 对于 Nodejs 中使用的 mongodb 查询,我应该使用什么格式在 JSON 中存储时间戳?

node.js - Mongoose:从基于另一个模型的结果中排除对象

java - 如何在没有时区的mongo DB中插入日期

go - Go中是否可以对一个函数的多个返回值进行类型转换?

linux - 从 Go 访问 Linux 网络 API

mongodb - 按值排序项目 mongodb