戈兰和mongo在mgo中查询

时间:2016-07-21 16:04:22

标签: mongodb go

所以我有这个mongo db查询工作正常:

db.levels.aggregate([{
         $match: {
                  "_id": {$lt: ObjectId("56410480f91e505237902dae")}
         },
},     
        { $group: 
            {             
                 "_id": {"title": "Level 11"},
                 "totalAmount": { $sum: "$rewardCoins"}
            }
        } 
])

它应该在给出id之前得到所有行,并且基于rewardCoins计算总和。

现在在golang中使用mgo我正在努力让这个工作......我会使用管道,但我的结果集是空的。

pipe := c.Pipe([]bson.M{{"$match": bson.M{"_id": bson.M{"$lt": level.ID}},
        "$group": bson.M{"_id":"$title", "totalReward": bson.M{"$sum": "$rewardCoins"}}}})

    res :=[]bson.M{}

    pipe.All(&res)

我在这里做错了什么?感谢。

更新

以下是我的简单行在db:

中的样子
{ "_id" : ObjectId("5613f5ad153678d113d01f4a"), "title" : "Level 1", "rewardCoins" : NumberLong(1000) }
{ "_id" : ObjectId("56159796153678d113d02d60"), "title" : "Level 2", "rewardCoins" : NumberLong(50000) }

2 个答案:

答案 0 :(得分:0)

Go中的结构与其他语言中的结构不同。如果您在每个大括号或逗号后按Enter键,请在Go需要的位置添加逗号,然后运行go fmt,即可获得一个bson.M

pipe := c.Pipe(
    []bson.M{
        {
            "$match": bson.M{
                "_id": bson.M{
                    "$lt": level.ID,
                },
            },
            "$group": bson.M{
                "_id": "$title",
                "totalReward": bson.M{
                    "$sum": "$rewardCoins",
                },
            },
        },
    },
)

另一种看待它的方法是Go中}, {之前没有$group,而JSON-y版本则有。{/ p>

这种缩进似乎是一个好主意;否则,眼睛会迷失在括号海中。

答案 1 :(得分:0)

我可能错了,但看起来查询中只有一个可能的问题是level.ID的类型。如果是字符串,则应使用ObjectID函数将其转换为bson.ObjectIdHex(string)

更新2

获取示例文档,但制作了自定义_id以确保它们符合规定。如果在您的数据库中,此代码无效,则表示您的文档_id不符合规定。即你已经添加了11级'在级别1'之前,或者在不同的计算机上添加了不同的级别,因为ObjectID中包含时间戳和计算机ID,并且不应该用作排序字段。正如有人在您的另一个问题中建议的那样 - 只需添加level数字字段和$match即可确保您选择正确的文档。

package main

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

func main() {
    session, err := mgo.Dial("mongodb://souser:123456@ds055855.mlab.com:55855/catalog")

    if err != nil {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("catalog").C("levels")

    /*
    Database structure
    { "_id" : ObjectId("56410480f91e505237902dab"), "title" : "Level 8", "rewardCoins" : NumberLong(25)}
    { "_id" : ObjectId("56410480f91e505237902dac"), "title" : "Level 9", "rewardCoins" : NumberLong(40)}
    { "_id" : ObjectId("56410480f91e505237902dad"), "title" : "Level 10", "rewardCoins" : NumberLong(55)}
    { "_id" : ObjectId("56410480f91e505237902dae"), "title" : "Level 11", "rewardCoins" : NumberLong(70)}
    */
    pipe := c.Pipe(
        []bson.M{
            bson.M{
                "$match": bson.M{
                    "_id": bson.M{
                        "$lt": bson.ObjectIdHex("56410480f91e505237902dae"),
                    },
                },
            },
            bson.M{
                "$group": bson.M{
                    "_id": bson.M {
                        "title": "Level 11",
                    },
                    "totalAmount": bson.M{"$sum": "$rewardCoins"},
                },
            },
        },
    )
    result := []bson.M{}
    err = pipe.All(&result) // [map[_id:map[title:Level 11] totalAmount:120]]
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d", result[0]["totalAmount"]) // 120
}