登录
首页 >  Golang >  Go问答

Golang与MongoDB值的错误导致更新问题

来源:stackoverflow

时间:2024-02-09 14:21:25 268浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《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删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>