I'm writing a db interface with mgo. Some documents in my model reference other documents.
type Child struct{
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
C string
}
type Parent struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
A string
B Child
}
child := Child{
Id: bson.NewObjectId(),
C: "panino"
}
parent := Parent{
Id: bson.NewObjectId(),
A: "Just a string",
B: child,
}
My aim is to:
- keep these documents embedded in the code,
- store parent in Parents collection with only a reference to child,
- store child in Children collection as a standalone document.
The following:
type Child struct{
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
C string `bson:"-"`
}
succeeds in 1 and 2 but only child.Id get stored in Children collection. I'm very new to Golang/mgo. I played a bit with Marshaling and Unmarshaling, but I don't quite understand how Getter and Setter work. I have the feeling they would do the trick though. Any clue?