CLI 与聚合管道相比呈现错误的数据
来源:stackoverflow
时间:2024-03-20 09:24:29 306浏览 收藏
在 MongoDB 中,使用聚合管道聚合数据时,Go 语言程序中的结果与命令行界面 (CLI) 中的结果不一致。管道在 CLI 中按预期工作,但 Go 语言程序返回的计数为 0。问题在于管道定义中使用了错误的类型(bson.M{} 而不是 bson.D{}),并且 feeds{} 结构与投影阶段定义不匹配。
问题内容
我在 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
中,您对字段使用一致的大小写。例如,指定全部小写 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
映射。
到这里,我们也就讲完了《CLI 与聚合管道相比呈现错误的数据》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习