mongodb - 聚合管道返回错误结果与 CLI

标签 mongodb go

我在 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{} .这是因为 bson.D{} 应该在顺序很重要的情况下使用,例如 MongoDB 命令。

您还可以将整个管道封装在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}},
    }},      
}

那么你不必在结构中指定bson映射:

type MyStruct struct {
    Type string 
    Total int
}

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

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

相关文章:

go - 是什么导致了这种 go​​lang os.Exec 行为(转义双引号)?

mongodb - Clojure - Monger Operators - 缺少日期转换的多字段连接和查询支持

node.js - 范围错误 : Invalid array buffer length Error - Reaction Commerce Installation

http - 在 go 中设置 http 处理程序

go - 多个返回值和 := in go

go - 在 Go (Golang) 中使用 http.filesystem 提供没有第三方库的字符串映射

java - mongodb 插入的空指针异常

mongodb - Graylog容器无法连接到MongoDB容器

mongodb - 有没有可以有效检测重复项的NoSQL数据库?

go - 构建 .go 包时出现 fatal error