$pull 在带有 mongo-driver 的 Go 中和在 Mongosh 中的工作方式不同
来源:stackoverflow
时间:2024-02-12 17:57:25 251浏览 收藏
一分耕耘,一分收获!既然都打开这篇《$pull 在带有 mongo-driver 的 Go 中和在 Mongosh 中的工作方式不同》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!
什么?
尝试从 mongo 文档中的对象数组中删除元素。
我使用: go版本go1.17.1 darwin/amd64; go.mongodb.org/mongo-driver v 1.8.4; mongodb 5.0.6 企业版
函数的简化版本,所有内容都尽可能简单:
func (r *repo) updatemodelbyid(ctx context.context, id string) error { objid, err := primitive.objectidfromhex(id) // i actually handle err here, it's nil filter := bson.d{{"_id", objid}} update = bson.d{{ key: "$pull", value: bson.e{ key: "data", value: bson.e{ key: "field_type", value: bson.e{ key: "$in", value: []string{"field_type_1", "field_type_2"}, }, }, }, }} log.debugln("update", update) result, err = cdm.gettemplatescollection().updateone(ctx, filter, update) // i actually handle err here, it's nil log.debugf("result_of_update: %+v", result) }
预期:
我提供的 id 文档在数组“data”中将不再包含字段“field_type”等于“field_type_1”或“field_type_2”的元素
得到:
debu[0023] update [{$pull {data {field_type {$in [field_type_1, field_type_2]}}}}] debu[0023] result_of_update: &{matchedcount:1 modifiedcount:0 upsertedcount:0 upsertedid:}
通过 mongosh 执行相同的命令:
db.templates.updateone( { _id: objectid("6228a89d621d19a2f7977d2f") }, { $pull: { data: {field_type: {$in: ["field_type_1", "field_type_2"]}}} } ) { acknowledged: true, insertedid: null, matchedcount: 1, modifiedcount: 1, upsertedcount: 0 }
并且 elem-s 从数组中消失了。
为什么会这样?
我确实注意到原生查询有单大括号(只是一个对象),而 go 代码使用 bson.d,从技术上讲,它是这些相同对象的数组 (bson.e) -> [{}]。
我尝试更改 mongosh 命令来测试:
db.templates.updateone( { _id: objectid("6228a89d621d19a2f7977d2f") }, [{ $pull: { data: {field_type: {$in: ["field_type_1", "field_type_2"]}}} }] ) mongoservererror: unrecognized pipeline stage name: '$pull'
在 go 中,我尝试使用 bson.e 而不是 bson.d (尽管每个文档建议使用后者的命令)并得到以下结果:
DEBU[0030] UPDATE {$pull {data {field_type {$in [field_type_1, field_type_2]}}}} ERRO[0030] cannot update model by id: update document must contain key beginning with '$' method=UpdateModelByID templateId=6228a89d621d19a2f7977d2f
正确答案
bson.D
使用键值对的有序列表对文档进行建模,其中键是属性名称,值是属性的值。
因此,如果您打算使用 bson.d
来建模文档,则始终必须在等效 shell 命令中存在文档的位置编写 bson.d
复合文字。
因此您的 update
文档必须如下所示:
update := bson.d{{ key: "$pull", value: bson.d{{ key: "data", value: bson.d{{ key: "field_type", value: bson.d{{ key: "$in", value: []string{"field_type_1", "field_type_2"}, }}}, }}, }}, }
如果从复合文字中省略命名字段,它会简化为:
update := bson.d{ {"$pull", bson.d{{ "data", bson.d{{ "field_type", bson.d{{ "$in", []string{"field_type_1", "field_type_2"}, }}}, }}, }}, }
是的,有点难看。请注意,如果元素的顺序并不重要,那么使用 bson.M
值来定义文档(它是一个地图)要容易得多。详情见bson.D vs bson.M for find queries
您的 update
可能类似于使用 bson.m
定义的:
update := bson.M{ "$pull": bson.M{ "data": bson.M{ "field_type": bson.M{ "$in": []string{"field_type_1", "field_type_2"}, }, }, }, }
终于介绍完啦!小伙伴们,这篇关于《$pull 在带有 mongo-driver 的 Go 中和在 Mongosh 中的工作方式不同》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
-
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次学习