亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

聚合管道返回錯誤結果與 CLI

聚合管道返回錯誤結果與 CLI

Go
holdtom 2023-06-12 15:41:46
我在 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}
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

首先,使用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您對字段使用一致的大小寫。例如,指定所有小寫type和/totalfeeds或所有大寫Type和TotalFeeds。


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。


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號