我在mongo中有一个集合,在该集合上运行以下查询

db.feeds.aggregate({"$match":{createdat:"20190203"}}, {"$group": {_id: {"type": "$type"}, total: {$sum: 1} }},{"$project":   {"type": "$_id.type", "tot": "$total", "_id": 0} }   )

它按预期工作并返回,
{ "type" : "f", "tot" : 1 }
{ "type" : "ebm", "tot" : 1 }
{ "type" : "b", "tot" : 3 }

但是,当我尝试在Golang中复制管道时,如下所示:
    pipeline := []bson.M{
    // match
    {"$match": bson.M{"createdat": when}},
    // group
    {"$group": bson.M{
        "_id":        bson.M{"type": "$type"}, // "$fieldname" - return the field
        "TotalFeeds": bson.M{"$sum": 1}}},
    // project
    {"$project": bson.M{"type": "$_id.type", // project selects subset of fields
        "TotalFeeds": "$TotalFeeds", // rename fiedls
        "_id":        0}},           // 0 means not show _id
}

返回的计数为0。
map[$match:map[createdat:20190203]] map[$group:map[TotalFeeds:map[$sum:1] _id:map[type:$type]]] map[$project:map[type:$_id.type TotalFeeds:$TotalFeeds _id:0]]]
{f 0  }
{ebm 0  }
{b 0  }
[{f 0  } {ebm 0  } {b 0  }]

在我在Golang中使用的整个功能下面:
func CountFeeds(when string) Feeds {

    ctx, _ := context.WithTimeout(context.Background(), 60*time.Second)

    pipeline := []bson.M{
        // match
        {"$match": bson.M{"createdat": when}},
        // group
        {"$group": bson.M{
            "_id":        bson.M{"type": "$type"}, // "$fieldname" - return the field
            "TotalFeeds": bson.M{"$sum": 1}}},
        // project
        {"$project": bson.M{"type": "$_id.type", // project select subset of fields
            "TotalFeeds": "$TotalFeeds", // rename fiedls
            "_id":        0}},           // 0 means not show _id
    }


    fmt.Println(pipeline)
    curs, err := db.Collection("feeds").Aggregate(ctx, pipeline)
    utilities.Catch(err)

    defer curs.Close(ctx)

    element := Feeds{}
    e := []Feeds{}
    for curs.Next(ctx) {
        err := curs.Decode(&element)
        fmt.Println(element)
        utilities.Catch(err)
        e = append(e, element)
    }

    fmt.Println(e)
    return element
}

最佳答案

首先,使用bson.D{}而不是bson.M{}。这是因为在顺序很重要的情况下(例如MongoDB命令)应使用bson.D{}

您还可以将整个管道封装在mongo.Pipeline中。例如:

pipeline := mongo.Pipeline{
    {{"$match", bson.D{{"createdata", 10}}}},
    {{"$group", bson.D{
        {"_id",        bson.D{{"type", "$type"}}},
        {"TotalFeeds", bson.D{{"$sum", 1}}},
    }}},
    {{"$project", bson.D{
        {"type", "$_id.type"},
        {"TotalFeeds", "$TotalFeeds"},
        {"_id", 0}},
    }},
}

检查您的Feeds{}结构映射。确保您指定bson映射,即:
type Feeds struct {
    Type string `bson:"type"`
    TotalFeeds int `bson:"TotalFeeds"`
}

或者,在投影阶段$project中,对字段使用一致的大小写。例如,指定所有小写的typetotalfeeds或所有大写的TypeTotalFeeds
pipeline := mongo.Pipeline{
    {{"$match", bson.D{{"createdata", 10}}}},
    {{"$group", bson.D{
        {"_id",        bson.D{{"type", "$type"}}},
        {"totalfeeds", bson.D{{"$sum", 1}}},
    }}},
    {{"$project", bson.D{
        {"type", "$_id.type"},
        {"totalfeeds", "$totalfeeds"},
        {"_id", 0}},
    }},
}

然后,您不必在struct中指定bson映射:
type MyStruct struct {
    Type string
    Total int
}

因此,要么在您的结构中使用一致的字段名大小写,要么显式提供bson映射。

关于mongodb - 聚合管道与CLI相比返回错误结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54974821/

10-15 07:30