I want to change stu1
to stu3
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Student struct {
Name string `bson:"name"`
Age string `bson:"age"`
}
type Class struct {
Id string `bson:"_id"`
Student []Student `bson:"student"`
}
col := mongosession.DB("test").C("class")
stu1 := Student{"jack", "18"}
stu2 := Student{"rose", "16"}
class := Class{Id: "123", Student: []Student{stu1, stu2}}
col.Insert(class)
stu3 := Student{"lisi", "14"}
How do I do the update? Is it like the following
col.Update(bson.M{"_id": "123"},
bson.M{"$set": bson.M{"student": ??????}})
Any help will be appreciated!
You can use the $set
operator and the dot notation :
err := col.Update(
bson.M{"_id": "123"},
bson.M{
"$set": bson.M{
"student.0": &stu3
}
})