Home > Backend Development > Golang > Update array in mongdbo document

Update array in mongdbo document

王林
Release: 2024-02-09 14:18:19
forward
919 people have browsed it

更新 mongdbo 文档中的数组

php Xiaobian Yuzai will introduce to you how to update an array in a MongoDB document. MongoDB is a non-relational database whose document structure is very flexible and can contain various types of data, including arrays. In actual development, we often need to update the data stored in the array. This article will explain in detail how to use MongoDB's update operator to update the array in the document, as well as some common usage scenarios and precautions. Whether you are new to MongoDB or an experienced developer, you can gain useful knowledge and practical experience from this article.

Question content

I'm trying to update an array item in a mongo document.

event is the document, I find it by the event id, then I need to identify which object in the task array needs to be changed by identifying the task name.

Know where I went wrong

Event structure

type event struct {
eventid              string       `json:"eventid" bson:"eventid"`
eventowner           string       `json:"eventowner" bson:"eventowner"`
eventtitle           string       `json:"eventtitle" bson:"eventtitle"`
eventdatetime        string       `json:"eventdatetime" bson:"eventdatetime"`
eventlocation        eventaddress `json:"eventlocation" bson:"eventlocation"`
eventtotalticket     int          `json:"eventtotalticket" bson:"eventtotalticket"`
eventavailableticket int          `json:"eventavailableticket" bson:"eventavailableticket"`
eventcoverimage      string       `json:"eventcoverimage" bson:"eventcoverimage"`
eventdescription     string       `json:"eventdescription" bson:"eventdescription"`
lat                  string       `json:"lat" bson:"lat"`
long                 string       `json:"long" bson:"long"`
task                 []task       `json:"task" bson:"task"`
}
Copy after login

Task structure:

type task struct {
tasktitle    string  `json:"tasktitle" bson:"tasktitle"`
initalbudget float32 `json:"initialbudget" bson:"initialbudget"`
supplier     string  `json:"suppid" bson:"suppid"`
agreedprice  float32 `json:"agreedprice" bson:"agreedprice"`
iscomplete   bool    `json:"iscomplete" bson:"iscomplete"`
}
Copy after login

This is what I want to do

func (e *EventServiceImpl) UpdateTask(eventid *string, task *models.Task) error {

    //filter := bson.D{bson.E{Key: "eventid", Value: eventid}, bson.E{Key: "tasktitle", Value: task.TaskTitle}}
    filter := bson.M{"$and": []bson.M{{"eventid": eventid}, {"task": bson.E{Key: "tasktitle", Value: task.TaskTitle}}}}
    update := bson.D{bson.E{Key: "$set", Value: bson.D{bson.E{Key: "task", Value: bson.M{"tasktitle": task.TaskTitle, "initialbudget": task.InitalBudget, "suppid": task.Supplier, "agreedprice": task.AgreedPrice, "iscomplete": task.IsComplete}}}}}
    result, _ := e.eventcollection.UpdateOne(e.ctx, filter, update)
    if result.MatchedCount != 1 {
        return errors.New("No matched document found to update")
    }
    return nil

}
Copy after login

I keep getting "No matching document found to update"

Solution

Here is the solution

func (e *EventServiceImpl) UpdateTask(eventid *string, task *models.Task) error {
    filter := bson.M{"eventid": eventid, "task": bson.M{"$elemMatch": bson.M{"tasktitle": task.TaskTitle}}}
    update := bson.M{"$set": bson.M{"task.$.initialbudget": task.InitalBudget, "task.$.suppid": task.Supplier, "task.$.agreedprice": task.AgreedPrice, "task.$.iscomplete": task.IsComplete}}
    result, err := e.eventcollection.UpdateOne(e.ctx, filter, update)
    if err != nil {
        return err
    }
    if result.MatchedCount != 1 {
        return errors.New("No matched document found to update")
    }
    return nil
}
Copy after login

The above is the detailed content of Update array in mongdbo document. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template