Golang与MongoDB值的错误导致更新问题
来源:stackoverflow
2024-02-09 14:21:25
0浏览
收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Golang与MongoDB值的错误导致更新问题》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
问题内容
我正在尝试更新我的 mongo 文档,但我不断收到错误,我似乎无法理解该错误。这就是我正在尝试做的事情
type updatecarpooldto struct {
carpoolid string `json:"carpoolid" bson:"carpoolid"`
passenger passenger `bson:"passenger" json:"passenger"`
}
type passenger struct {
user user `bson:"user" json:"user"`
numberofseats int `json:"numberofseats" bson:"numberofseats"`
pickup location `json:"pickup" bson:"pickup"`
dropoff location `json:"dropoff" bson:"dropoff"`
doortodooroption bool `bson:"doortodooroption" json:"doortodooroption"`
luggagesize string `bson:"luggagesize" json:"luggagesize"`
}
type carpool struct {
id string `json:"id,omitempty" bson:"_id,omitempty"`
seatsavailable int `json:"seatsavailable" bson:"seatsavailable"`
passengers []*passenger `bson:"passengers" json:"passengers"`
createdat time.time `json:"createdat" bson:"createdat"`
updatedat time.time `json:"updatedat" bson:"updatedat"`
...
}
func (r *carpoolrepository) addpassanger(dto *updatecarpooldto) (*carpool, *errors.error) {
ctx, cancel := context.withtimeout(context.background(), mongoquerytimeout)
defer cancel()
log.println("the dto obj:::", dto)
var result *carpool
filter := bson.m{"_id": dto.carpoolid}
update := bson.m{
"$set": bson.m{
"passangers": bson.m{
"$push": dto.passenger,
},
"seatsavailable": bson.m{
"$subtract": []interface{}{
"$seatsavailable",
dto.passenger.numberofseats,
},
},
},
"$currentdate": bson.m{"updatedat": true},
}
log.println("the dto obj:::", "got here 1")
options := options.findoneandupdate().setreturndocument(1)
log.println("the dto obj:::", "got here 2")
if err := r.collection.findoneandupdate(ctx, filter, update, options).decode(&result); err != nil {
log.println("the dto obj:::", "got here 3")
if err == mongo.errnodocuments {
// failing here
log.println("the dto obj:::", "got here 4")
return nil, errors.newnotfounderror("request not found")
}
log.println("the dto obj:::", "got here 5")
log.println("the dto obj 5 err:::", err.error())
return nil, errors.new(err.error())
}
log.println("the dto obj:::", "got here 6")
return result, nil
}
我用来调试的日志语句打印以下内容
THE DTO OBJ::: &{91e7a42c-xxxx {{Ebene 0x14000392c00 Obey 5120000000 } 1 {312 Main St 3 43.xxx -79.xxxx Ontario Toronto N2J 223 Canada } {978 Elm St 2 43.xxxx -80.xxx Ontario Toronto 2B2 111 Canada } true MEDIUM}} 2023/06/15 11:32:45
THE DTO OBJ::: GOT HERE 1 2023/06/15 11:32:45
THE DTO OBJ::: GOT HERE 2 2023/06/15 11:32:45
THE DTO OBJ::: GOT HERE 3 2023/06/15 11:32:45
THE DTO OBJ::: GOT HERE 5 2023/06/15 11:32:45
THE DTO OBJ 5 err::: error decoding key seatsAvailable: cannot decode embedded document into an integer type
可根据要求提供更多代码。
正确答案
我整理了一个小示例来展示正常更新(使用 findoneandupdate 方法完成)和使用 aggregate 方法进行的聚合操作之间的区别。作为前提,我通过从结构中删除字段并对一些值进行硬编码来简化了一些代码。如果我错过了什么,请告诉我,我会更新我的答案!
首先分享一下代码:
package main
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Passenger struct {
NumberOfSeats int `json:"numberOfSeats" bson:"numberOfSeats"`
DoorToDoorOption bool `bson:"doorToDoorOption" json:"doorToDoorOption"`
LuggageSize string `bson:"luggageSize" json:"luggageSize"`
}
type Carpool struct {
ID string `json:"id,omitempty" bson:"_id,omitempty"`
SeatsAvailable int `json:"seatsAvailable" bson:"seatsAvailable"`
Passengers []*Passenger `bson:"passengers" json:"passengers"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
}
func main() {
clientOptions := options.Client().ApplyURI("mongodb://root:root@localhost:27017")
mongoClient, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
panic(err)
}
defer mongoClient.Disconnect(context.Background())
appDB := mongoClient.Database("appDB")
demoCollection := appDB.Collection("demoCollection")
// clean up data - troubleshooting purposes
defer func() {
demoCollection.DeleteMany(context.Background(), bson.M{})
}()
// seed data
carPool := &Carpool{
ID: "1",
SeatsAvailable: 4,
Passengers: []*Passenger{
{NumberOfSeats: 1, DoorToDoorOption: false, LuggageSize: "small"},
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
insertRes, err := demoCollection.InsertOne(context.Background(), carPool)
if err != nil {
panic(err)
}
// fetch data
fmt.Println("before update")
cursor, err := demoCollection.Find(context.Background(), bson.M{"_id": insertRes.InsertedID})
if err != nil {
panic(err)
}
var carPools []bson.M
if err = cursor.All(context.Background(), &carPools); err != nil {
panic(err)
}
for _, v := range carPools {
fmt.Println(v)
}
// set data
filter := bson.M{"_id": insertRes.InsertedID}
update := bson.D{
bson.E{
Key: "$push",
Value: bson.D{
bson.E{
Key: "passengers",
Value: &Passenger{NumberOfSeats: 2, DoorToDoorOption: true, LuggageSize: "medium"},
},
},
},
}
updateOptions := options.FindOneAndUpdate().SetReturnDocument(1)
demoCollection.FindOneAndUpdate(context.Background(), filter, update, updateOptions)
// aggregate update
pipe := []bson.M{
{
"$addFields": bson.M{ // if the field already exists, it overwrites the value
"seatsAvailable": bson.M{
"$subtract": []string{"$seatsAvailable", "$seatsAvailable"},
},
},
},
}
cursor, err = demoCollection.Aggregate(context.Background(), pipe)
if err != nil {
panic(err)
}
if err = cursor.All(context.Background(), &carPools); err != nil {
panic(err)
}
fmt.Println("after update")
for _, v := range carPools {
fmt.Println(v)
}
}
我将仅详细介绍更新和聚合函数。
更新
更新功能分为以下几个部分:
- 设置用于获取要更新的文档的过滤器
- 设置
$push运算符以将项目添加到集合 - 设置要添加的值
这里没有什么可添加的。让我们切换到聚合函数。
聚合
对于此操作,您必须设置要使用的管道。该管道用于 $addfiels 阶段。
请注意,如果您要添加一个已经存在的字段,该字段将被覆盖。我选择使用它是因为我对此很满意。
最后,您必须设置要使用的运算符(在本例中为 $subtract 运算符)并提供表示要减去的数字的字符串数组。感谢 $subtract 运算符,您可以从第一个值中减去第二个值。
我添加了一些日志用于演示目的。您可以轻松地看到代码更新了记录。
请告诉我这是否有助于澄清您的问题,谢谢!
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
非阻塞休眠在 Go 中的应用
- 上一篇
- 非阻塞休眠在 Go 中的应用
- 下一篇
- 如何设定切片的长度?
查看更多
最新文章
-
- Golang · Go问答 | 26分钟前 | 连接池 · 性能排查 · Go问答 · net/http Go HTTP/2 MaxConcurrentStreams StrictMaxConcurrentRequests 请求排队
- Go HTTP/2 流并发限制导致请求排队的调参思路
- 188浏览 收藏
-
- Golang · Go问答 | 1小时前 | net/http · Go问答 · Go 单页应用 SPA http.FileServer embed.FS index.html
- Go http.FileServer 为单页应用提供回退文件
- 106浏览 收藏
-
- Golang · Go问答 | 1小时前 | HTTP · Cookie · net/http · Go问答 · cookie Go net/http CookiesNamed Request.Cookies
- Go Request.Cookies 处理同名 Cookie 的读取顺序
- 102浏览 收藏
-
- Golang · Go问答 | 2小时前 |
- Go Cookie SameSite 配置在跨站请求中的边界
- 111浏览 收藏
-
- Golang · Go问答 | 2小时前 | HTTP · net/http · Go问答 · Go net/http 流式响应 ResponseWriter HTTP Trailer
- Go HTTP Trailer 在流式响应中的声明顺序
- 215浏览 收藏
-
- Golang · Go问答 | 3小时前 | net/http · Go问答 · 流式响应 · Go FLUSH ResponseController ErrNotSupported
- Go ResponseController Flush 返回不支持时的兼容处理
- 425浏览 收藏
-
- Golang · Go问答 | 3小时前 | HTTP · go · Go handler Request.Body
- Go Handler 读取请求体后下游为空的修复方案
- 442浏览 收藏
-
- Golang · Go问答 | 3小时前 |
- Go HTTP 中间件重复写响应头的定位方法
- 270浏览 收藏
-
- Golang · Go问答 | 4小时前 |
- Go ServeMux 路径变量冲突时的路由选择排查
- 350浏览 收藏
-
- Golang · Go问答 | 5小时前 | go · Go os/exec ErrWaitDelay WaitDelay
- Go exec.WaitDelay 为什么会返回 ErrWaitDelay
- 333浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 256次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 299次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 275次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 254次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 61次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览
-
- Go sql.Tx提交成功前读取结果导致事务边界混乱的修复方法
- 2026-09-20 501浏览
-
- Go select 用 time.After 做超时有什么资源代价
- 2026-09-10 501浏览
