I have structs like:
type Meet struct {
Title string `json:title`
Time time.Time `json:time`
Host string `json:host`
Crowd []string `json:crowd`
Geo Location `json:location`
Invoice []Bill `json:invoice`
}
type User struct {
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
Name string `json:name`
Phone string `json:phone`
Email string `json:email`
Vc string `json:vc`
Status int8 `json:status`
Avatar string `json:avatar`
FriendList []bson.ObjectId `json:friendlist`
Meetings []Meet `json:meetings`
Requests []Request `json:request`
}
and want to update Invoice of a Meet (like: User.Meetings[0].Invoice) my code is like:
query := bson.M{
"_id": bson.ObjectIdHex(personId),
"Meetings.Title": Title,
"Meetings.Geo": Geo,
}
update := bson.M{
"$set": bson.M{
"Meetings.$.Invoice": updateInvoice,
},
}
updateErr = collection.Update(query, update)
what I'v got was only not found error.commenting Meetings.Geo didn't help and cause the same reason.not found. is this something wrong with my query or what?
The fields in your query should be meetings.title and meetings.geo. I just tested it with one of my DBs and case of the fields matters. In your update, Meetings should be meetings. The names are taken from the struct item tag name not the struct item name. For example
struct test {
ID bson. bson.ObjectId `bson:"_id"`
TestString string `bson:"ts"`
Amount int `bson:"am"`
}
query := bson.M{"ts": "test", "am": 2}
You cannot have omitempty against the _id as the _id field has to exist.