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