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

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

Updating an array within mongdbo document [Golang]

问题

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

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

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

Event结构体:

  1. type Event struct {
  2. EventId string `json:"eventid" bson:"eventid"`
  3. EventOwner string `json:"eventowner" bson:"eventowner"`
  4. EventTitle string `json:"eventtitle" bson:"eventtitle"`
  5. EventDateTime string `json:"eventdatetime" bson:"eventdatetime"`
  6. EventLocation EventAddress `json:"eventlocation" bson:"eventlocation"`
  7. EventTotalTicket int `json:"eventtotalticket" bson:"eventtotalticket"`
  8. EventAvailableTicket int `json:"eventavailableticket" bson:"eventavailableticket"`
  9. EventCoverImage string `json:"eventcoverimage" bson:"eventcoverimage"`
  10. EventDescription string `json:"eventdescription" bson:"eventdescription"`
  11. Lat string `json:"lat" bson:"lat"`
  12. Long string `json:"long" bson:"long"`
  13. Task []Task `json:"task" bson:"task"`
  14. }

Task结构体:

  1. type Task struct {
  2. TaskTitle string `json:"tasktitle" bson:"tasktitle"`
  3. InitalBudget float32 `json:"initialbudget" bson:"initialbudget"`
  4. Supplier string `json:"suppid" bson:"suppid"`
  5. AgreedPrice float32 `json:"agreedprice" bson:"agreedprice"`
  6. IsComplete bool `json:"iscomplete" bson:"iscomplete"`
  7. }

这是我尝试的代码:

  1. func (e *EventServiceImpl) UpdateTask(eventid *string, task *models.Task) error {
  2. //filter := bson.D{bson.E{Key: "eventid", Value: eventid}, bson.E{Key: "tasktitle", Value: task.TaskTitle}}
  3. filter := bson.M{"$and": []bson.M{{"eventid": eventid}, {"task": bson.E{Key: "tasktitle", Value: task.TaskTitle}}}}
  4. 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}}}}}
  5. result, _ := e.eventcollection.UpdateOne(e.ctx, filter, update)
  6. if result.MatchedCount != 1 {
  7. return errors.New("No matched document found to update")
  8. }
  9. return nil
  10. }

我总是收到"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

  1. type Event struct {
  2. EventId string `json:"eventid" bson:"eventid"`
  3. EventOwner string `json:"eventowner" bson:"eventowner"`
  4. EventTitle string `json:"eventtitle" bson:"eventtitle"`
  5. EventDateTime string `json:"eventdatetime" bson:"eventdatetime"`
  6. EventLocation EventAddress `json:"eventlocation" bson:"eventlocation"`
  7. EventTotalTicket int `json:"eventtotalticket" bson:"eventtotalticket"`
  8. EventAvailableTicket int `json:"eventavailableticket" bson:"eventavailableticket"`
  9. EventCoverImage string `json:"eventcoverimage" bson:"eventcoverimage"`
  10. EventDescription string `json:"eventdescription" bson:"eventdescription"`
  11. Lat string `json:"lat" bson:"lat"`
  12. Long string `json:"long" bson:"long"`
  13. Task []Task `json:"task" bson:"task"`
  14. }

Task Struct:

  1. type Task struct {
  2. TaskTitle string `json:"tasktitle" bson:"tasktitle"`
  3. InitalBudget float32 `json:"initialbudget" bson:"initialbudget"`
  4. Supplier string `json:"suppid" bson:"suppid"`
  5. AgreedPrice float32 `json:"agreedprice" bson:"agreedprice"`
  6. IsComplete bool `json:"iscomplete" bson:"iscomplete"`
  7. }

Here's what i'm trying to do

  1. func (e *EventServiceImpl) UpdateTask(eventid *string, task *models.Task) error {
  2. //filter := bson.D{bson.E{Key: "eventid", Value: eventid}, bson.E{Key: "tasktitle", Value: task.TaskTitle}}
  3. filter := bson.M{"$and": []bson.M{{"eventid": eventid}, {"task": bson.E{Key: "tasktitle", Value: task.TaskTitle}}}}
  4. 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}}}}}
  5. result, _ := e.eventcollection.UpdateOne(e.ctx, filter, update)
  6. if result.MatchedCount != 1 {
  7. return errors.New("No matched document found to update")
  8. }
  9. return nil
  10. }

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

答案1

得分: 0

这是解决方案:

  1. func (e *EventServiceImpl) UpdateTask(eventid *string, task *models.Task) error {
  2. filter := bson.M{"eventid": eventid, "task": bson.M{"$elemMatch": bson.M{"tasktitle": task.TaskTitle}}}
  3. update := bson.M{"$set": bson.M{"task.$.initialbudget": task.InitalBudget, "task.$.suppid": task.Supplier, "task.$.agreedprice": task.AgreedPrice, "task.$.iscomplete": task.IsComplete}}
  4. result, err := e.eventcollection.UpdateOne(e.ctx, filter, update)
  5. if err != nil {
  6. return err
  7. }
  8. if result.MatchedCount != 1 {
  9. return errors.New("未找到匹配的文档进行更新")
  10. }
  11. return nil
  12. }
英文:

Here was the solution

  1. func (e *EventServiceImpl) UpdateTask(eventid *string, task *models.Task) error {
  2. filter := bson.M{"eventid": eventid, "task": bson.M{"$elemMatch": bson.M{"tasktitle": task.TaskTitle}}}
  3. update := bson.M{"$set": bson.M{"task.$.initialbudget": task.InitalBudget, "task.$.suppid": task.Supplier, "task.$.agreedprice": task.AgreedPrice, "task.$.iscomplete": task.IsComplete}}
  4. result, err := e.eventcollection.UpdateOne(e.ctx, filter, update)
  5. if err != nil {
  6. return err
  7. }
  8. if result.MatchedCount != 1 {
  9. return errors.New("No matched document found to update")
  10. }
  11. return nil
  12. }

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:

确定