更新MongoDB文档中的数组 [Golang]

huangapple go评论69阅读模式
英文:

Updating an array within mongdbo document [Golang]

问题

我正在尝试更新Mongo文档中的数组项。

Event是文档,我通过事件ID查找它,然后需要通过任务名称来确定需要更改的任务数组中的对象。

你有任何关于我哪里出错的想法吗?

Event结构体:

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"`
}

Task结构体:

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"`
}

这是我尝试的代码:

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

}

我总是收到"No matched document found to update"的错误提示。

英文:

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

Event is the Document, which i'm finding with the event id, then i need to identify which object in the task array needs to be changed by identifying the name of the task.

Any idea where i'm going wrong

Event Stuct

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"`
}

Task Struct:

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"`
}

Here's what i'm trying 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

}

I always receive the "No matched document found to update"

答案1

得分: 0

这是解决方案:

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("未找到匹配的文档进行更新")
	}
	return nil
}
英文:

Here was 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
}

huangapple
  • 本文由 发表于 2023年3月28日 05:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75860468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定