当前位置:首页 > 文章列表 > Golang > Go问答 > Golang与MongoDB值的错误导致更新问题

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)
    }
}

我将仅详细介绍更新和聚合函数。

更新

更新功能分为以下几个部分:

  1. 设置用于获取要更新的文档的过滤器
  2. 设置 $push 运算符以将项目添加到集合
  3. 设置要添加的值

这里没有什么可添加的。让我们切换到聚合函数。

聚合

对于此操作,您必须设置要使用的管道。该管道用于 $addfiels 阶段。

请注意,如果您要添加一个已经存在的字段,该字段将被覆盖。我选择使用它是因为我对此很满意。

最后,您必须设置要使用的运算符(在本例中为 $subtract 运算符)并提供表示要减去的数字的字符串数组。感谢 $subtract 运算符,您可以从第一个值中减去第二个值。

我添加了一些日志用于演示目的。您可以轻松地看到代码更新了记录。

请告诉我这是否有助于澄清您的问题,谢谢!

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
非阻塞休眠在 Go 中的应用非阻塞休眠在 Go 中的应用
上一篇
非阻塞休眠在 Go 中的应用
如何设定切片的长度?
下一篇
如何设定切片的长度?
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 笔灵AI生成答辩PPT:高效制作学术与职场PPT的利器
    笔灵AI生成答辩PPT
    探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
    28次使用
  • 知网AIGC检测服务系统:精准识别学术文本中的AI生成内容
    知网AIGC检测服务系统
    知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
    42次使用
  • AIGC检测服务:AIbiye助力确保论文原创性
    AIGC检测-Aibiye
    AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
    39次使用
  • 易笔AI论文平台:快速生成高质量学术论文的利器
    易笔AI论文
    易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
    51次使用
  • 笔启AI论文写作平台:多类型论文生成与多语言支持
    笔启AI论文写作平台
    笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
    42次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码