Say for example I had the following struct:
type Article struct {
Title string `form"title" json:"title"`
Categories []*Category
}
How would I go about adding a new category?
Sorted it using:
change := mgo.Change{
Update: bson.M{"$push": bson.M{"categories": cat}},
}
_, err := repo.collection.FindId(bson.ObjectIdHex(article)).Apply(change, nil)
if err != nil {
panic(err)
}
Update the Article struct as :
type Article struct {
ArticleId string `bson:"_id"`
Title string `form"title" json:"title,omitempty"`
Categories []Category `json:"category,omitempty"`
}
Your Query accordingly:
data := model.Category{
CategoryId : "yourText",
Product : "productName,
...
}
selector := bson.M{"_id": "provideTheTitle"}
changes := bson.M{"$push": bson.M{"category": bson.M{"$each": []model.Category{data}}}}
err = c.Update(selector, changes)
It would be great if you can share your Category struct and include an field _id in your Article struct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.