[英]Relationships in mgo
I've written some simple program with golang and mgo. 我用golang和mgo写了一些简单的程序。 My question is how to properly to relationships in mgo. 我的问题是如何正确地处理mgo中的关系。
1st Approach: 第一种方法:
type User struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Friends []User `json:"friends" bson:"friends"`
}
" Friends " is a slice of Users. “ 朋友 ”是用户的一部分。 I can $push a pointer to a User and it just works fine. 我可以$推送一个指向用户的指针,它只是工作正常。 The thing is that I only want to store a reference to the user and not nesting it: 问题是我只想存储对用户的引用而不是嵌套它:
2nd Approach: 第二种方法:
type User struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Friends []bson.ObjectId `json:"friends" bson:"friends"`
}
This gives me the output I want - but now it's not visible from the struct which nested structs are referenced. 这给了我想要的输出 - 但是现在从结构中看不到哪些嵌套结构被引用了。 Does mgo provide some mechanism to deal with this? mgo是否提供了一些处理这个问题的机制?
mgo是一个db驱动程序库,而不是ORM ..我要做的是像第二个例子中那样使用ids数组(未导出,带小写)并且有一个Friends()方法,它通过这些id查询数据库并返回一个[]用户
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.