mgo中的模型关系

huangapple go评论64阅读模式
英文:

Model relationships in mgo

问题

我正在使用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,
}

我的目标是:

  1. 将这些文档嵌入到代码中,
  2. 将parent存储在Parents集合中,只包含对child的引用,
  3. 将child作为独立文档存储在Children集合中。

下面的代码可以实现1和2,但只有child.Id被存储在Children集合中。

type Child struct{
    Id       bson.ObjectId   `json:"_id,omitempty" bson:"_id,omitempty"`
    C        string          `bson:"-"`
}

我对Golang/mgo非常陌生。我尝试过一些编组和解组的操作,但我不太理解Getter和Setter的工作原理。不过我有一种感觉它们可能会起作用。有什么线索吗?

英文:

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:

  1. keep these documents embedded in the code,
  2. store parent in Parents collection with only a reference to child,
  3. 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?

答案1

得分: 2

你可能正在寻找bson:"omitempty"标签,而不是bson:"-"。前者只在字段为空时省略该字段,而不是始终省略。另外,你也可以使用第二个ChildReference类型,仅在引用中使用。在同一个集合中使用不同类型是可以的。

另外,需要注意的是,虽然在某些情况下会使用这种做法,但并不是在所有情况下都必须将集合名称存储在文档ID旁边。对于定义良好的模式,最常见的做法是仅存储文档ID本身(例如,使用{"person_id": 123},含义是清晰的)。

英文:

You are probably looking for the bson:",omitempty" tag instead of bson:"-". The former will omit the field only if it is empty, instead of at all times. Alternatively, you can also have a secondary ChildReference type that is used on references only. It's fine to use different types with the same collection.

As an aside, note that although that practice is used in some situations, you don't have to store the collection name next to the document id in all cases. The most common practice for well defined schemas is to store simply the document id (e.g. with {"person_id": 123} the meaning is clear).

huangapple
  • 本文由 发表于 2015年3月2日 21:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/28810662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定