英文:
How would I select all embedded documents of the same type in mgo Mongodb?
问题
我有一个使用mgo/mongodb的Go应用程序。我使用嵌入式文档而不是关系型文档。
所以我有...(为了简洁起见,某些代码已被省略)。
type User struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `form:"name" bson:"name" json:"name"`
Password string `form:"password" bson:"password,omitempty" json:"password" binding:"required"`
Email string `form:"email" bson:"email,omitempty" json:"email" binding:"required"`
Artists []Artist `form:"artists" bson:"artists,omitempty" json:"artists" inline`
Releases []Release `form:"releases" bson:"releases,omitempty" json:"releases" inline`
ContentFeed []Content `form:"content_feed" bson:"content_feed,omitempty" json:"content_feed" inline`
Profile Profile `form:"profile" bson:"profile,omitempty" json:"profile" inline`
TopTracks []Track `form:"top_tracks" bson:"top_tracks" json:"top_tracks" inline`
}
type Artist struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Title string `form:"title" bson:"title" json:"title"`
Genres string `form:"genres" bson:"genres" json:"genres"`
}
func (repo *ArtistRepo) GetArtists() ([]Artist, error) {
results := &[]Artist{}
err := repo.collection.Find(???).All(results)
return results, err
}
我正在尝试获取所有艺术家,实际上是从所有用户那里获取。但是我无法确定查询中需要什么?我简要地接触过Map/Reduce,但似乎不适用于我想做的事情。
英文:
I have a go application which uses mgo/mongodb. I'm using embedded documents rather than relational ones.
So I have... (some code redacted for brevity).
type User struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `form:"name" bson:"name" json:"name"`
Password string `form:"password" bson:"password,omitempty" json:"password" binding:"required"`
Email string `form:"email" bson:"email,omitempty" json:"email" binding:"required"`
Artists []Artist `form:"artists" bson:"artists,omitempty" json:"artists" inline`
Releases []Release `form:"releases" bson:"releases,omitempty" json:"releases" inline`
ContentFeed []Content `form:"content_feed" bson:"content_feed,omitempty" json:"content_feed" inline`
Profile Profile `form:"profile" bson:"profile,omitempty" json:"profile" inline`
TopTracks []Track `form:"top_tracks" bson:"top_tracks" json:"top_tracks" inline`
}
type Artist struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Title string `form:"title" bson:"title" json:"title"`
Genres string `form:"genres" bson:"genres" json:"genres"`
}
func (repo *ArtistRepo) GetArtists() ([]Artist, error) {
results := &[]Artist{}
err := repo.collection.Find(???).All(results)
return results, err
}
I'm trying to get all of the artists, from all of the users essentially. But I can't figure what I need in my query? I've touched briefly on Map/Reduce, but it didn't seem to apply to what I'm trying to do.
答案1
得分: 2
我认为你假设mgo
是一个“ORM”(对象关系映射)。但实际上它只是一种在Mongo中存储数据的简单方式。有三种不同的方法可以解决你的问题:
-
将不同类型的数据放入不同的集合中。这样,每个文档都是相同类型的。(集合类似于关系数据库中的“表”)。
-
使用标签为每个对象标记其类型(例如,在字段中存储对象类型),然后可以根据类型进行查询。
-
如果你感觉有冒险精神,你可以假设所有的艺术家都有流派,所有的用户都有个人资料。然后使用
$exists
来仅选择该类型。
第一种选项是通常的做法。你应该有具体的理由来选择第二种或第三种方法,因为它们可能会更慢。
英文:
I think you are assuming mgo
is an "ORM". But it's just a simple way to store data in Mongo. There are 3 different ways to fix your problem:
-
Put different types into different collections. That way, every document is the same type. (Collections are like "tables" in a relational database).
-
Tag each thing with it's type (i.e. store the object type in a field), then you can query on it.
-
If you are feeling dangerous, you can assume all Artists have Genres, and all Users have a Profile. Then use
$exists
to select only that type.
The first option is the usual way to do it. You should have specific reasons for doing #2 or #3, as they could be slower.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论