1
我正在用mgo編寫一個db接口。 我模型中的一些文檔引用其他文檔。mgo中的模型關係
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,
}
我的目標是:
- 保持這些文件嵌入代碼,
- 店父的家長收集只到子對象的引用,
- 店的孩子在兒童集合作爲一個獨立的文件。
以下:
type Child struct{
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
C string `bson:"-"`
}
成功在1和2,但只有child.Id獲取存儲在兒童集合。 我對Golang/mgo很新。我和Marshaling和Unmarshaling玩了一下,但我不太明白Getter和Setter是如何工作的。儘管如此,我有他們會這樣做的感覺。 任何線索?
結構標記'bson:「 - 」'告訴bson忽略Child的C字段。 – Logiraptor 2015-03-02 15:38:54
這是一個問題,因爲孩子也必須作爲獨立文檔存儲。 – Alain1405 2015-03-02 15:53:28