dsstjqsr631426 2018-02-06 04:42
浏览 97
已采纳

mongodb聚合在golang中给出错误

I want group methods types and count them according to their types.these methods are strings. I wrote below code.But it gives an error.

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)

It gives below error.

2018/02/06 09:58:33 http: panic serving 127.0.0.1:53973: runtime error: index out of range goroutine 92 [running]:

please help me to correct this

  • 写回答

1条回答 默认 最新

  • duanhan8757 2018-02-06 08:00
    关注

    Hi this is a sample code I modified to use pipelines with MgO golang. Watch It and try to apply it. package main

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

    Dataset in the 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
    }
    

    Out put

    Type: [{POST 2} {PUT 2} {GET 2}]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取豆瓣电影相关处理
  • ¥15 手机淘宝抓清除消息接口
  • ¥15 C#无selenium
  • ¥15 LD衰减计算的结果过大
  • ¥15 用机器学习方法帮助保险公司预测哪些是欺诈行为
  • ¥15 计算300m以内的LD衰减
  • ¥15 数据爬取,python
  • ¥15 怎么看 cst中一个面的功率分布图,请说明详细步骤。类似下图
  • ¥15 为什么我的pycharm无法用pyqt6的QtWebEngine
  • ¥15 FOR循环语句显示查询超过300S错误怎么办