2部分问题.
1是mongodb查询本身,接下来是如何在mgo中完成它.
如何查询类型类别的1个文档(结果应该是类型类别)在哪里slug: "general"?
我选择这种布局的原因是因为我读到了mongodb的优点是具有嵌入式"结构"的性能但是我担心我必须将"类别"和"论坛"作为自己的集合并重写大量代码,我想避免因为客户端的每个视图都需要访问这些模型,这将导致每个新页面加载(对于类别和论坛)的1-2个额外查询,并且使用mongodb的优势将消失.
后续问题是,我如何更新或删除一个特定的嵌入式文档?
有没有办法直接从mongodb获取类别文档,而无需分离文档或在Go中编写查找,更新,删除功能,以及如何?
这个结构:
{
"_id" : ObjectId("5303d1a2d6194c0f27000001"),
"name" : "darko",
"description" : "darko",
"subdomain" : "darko",
"domain" : "mango.dev",
"created" : ISODate("2014-02-18T21:33:22.115Z"),
"category" : "Brains",
"owner" : "52b1d74dd6194c0646000002",
"members" : [
"52b1d74dd6194c0646000002"
],
"categories" : [
{
"_id" : ObjectId("5303d1a2d6194c0f27000003"),
"name" : "Admin and Moderator Area",
"slug" : "admin-and-moderator-area",
"adminonly" : true,
"membersonly" : false,
"forums" : [
{
"_id" : ObjectId("5303d1a2d6194c0f27000005"),
"name" : "Admin Discussion",
"slug" : "admin-discussion",
"text" : "This is the main forum for administrative topics."
}
]
},
{
"_id" : ObjectId("5303d1a2d6194c0f27000002"),
"name" : "General",
"slug" : "general",
"adminonly" : false,
"membersonly" : false,
"forums" : [
{
"_id" : ObjectId("5303d1a2d6194c0f27000004"),
"name" : "General Discussion",
"slug" : "general-discussion",
"text" : "Talk about everything and anything here in this general discussion forum"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
或者去:
Community struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `json:"name"`
Description string `bson:",omitempty" json:"description"`
Subdomain string `bson:",omitempty" json:"subdomain"`
Domain string `json:"domain"`
Created time.Time `json:"created"`
Category string `json:"category"`
Owner interface{} `json:"owner"` //userid
Members []interface{} `json:"members"` //userid
Moderators []interface{} `bson:",omitempty" json:"moderators"` //userid
Logo string `bson:",omitempty" json:"logo"` // relative path to file
Stylesheets []string `bson:",omitempty" json:"stylesheets"` // absolute path to files
Javascripts []string `bson:",omitempty" json:"javascripts"` // absolute path to files
Categories []*Category `json:"categories"`
}
Category struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
AdminOnly bool `json:"-"`
MembersOnly bool `json:"-"`
Forums []*Forum `json:"forums"`
}
Forum struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Text string `json:"text"`
Moderators []interface{} `bson:",omitempty" json:"moderators"` //userid
}
Run Code Online (Sandbox Code Playgroud)
1.
目前,MongoDB没有内置方式返回子文档,只能返回文档的投影.话虽这么说,您仍然可以使用投影找到您要查找的数据.
在MongoDB的手册为您提供了一个非常类似的例子.您应该进行的查询是:
db.coll.find({}, {categories:{ $elemMatch: {"slug":"general"}}})
Run Code Online (Sandbox Code Playgroud)
2.
使用mgo,使用Select处理投影部分.该氧化镁文档状态:
func(q*Query)选择(选择器接口{})*查询
选择允许选择应为找到的结果检索哪些字段.
没有尝试过,它应该看起来像这样:
err := collection.Find(nil).Select(bson.M{"categories": bson.M{"$elemMatch": bson.M{"slug": "general"}}}).One(&result)
Run Code Online (Sandbox Code Playgroud)
为了获得嵌套的Category对象,您可以让它result成为包含结构:
type CategoryContainer struct {
Categories []Category{} // Or even [1]Category{} in this case
}
Run Code Online (Sandbox Code Playgroud)
然后简单地获取类别:
category := result.Categories[0]
Run Code Online (Sandbox Code Playgroud)
3.
为了更新子文档,已经有很好的帖子,例如:
| 归档时间: |
|
| 查看次数: |
9095 次 |
| 最近记录: |