6

2 部分问题。

1是mongodb查询本身,接下来是mgo中怎么做。

如何查询 1 个类别类型的文档(结果应该是类别类型),其中slug: "general"

我选择这个布局的原因是因为我读到 mongodb 的优势是嵌入“结构”的性能但是我担心我必须让“类别”和“论坛”成为自己的集合并重写很多代码,我想避免那是因为客户端的每个视图都需要访问这些模型,并且在每个新页面加载(对于类别和论坛)上都会导致 1-2 个额外的查询,并且使用 mongodb 的优势将消失。

接下来的问题是,我将如何更新或删除一个特定的嵌入文档?

有没有办法直接从 mongodb 获取类别文档,而无需分离文档或在 Go 中编写 find、update、delete 函数,以及如何?

这个结构:

{
    "_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"
                }
            ]
        }
    ]
}

或继续:

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
}
4

2 回答 2

13

1.

目前,MongoDB 没有内置方法返回子文档,您只能返回文档的投影。话虽如此,您仍然可以使用投影找到所需的数据。

MongoDB 手册为您提供了一个非常相似的示例。您应该进行的查询是:

db.coll.find({}, {categories:{ $elemMatch: {"slug":"general"}}})

2.

使用mgo,使用 Select 处理投影部分。mgo文档指出:

func (q *Query) Select(选择器接口{}) *Query

选择允许选择应该为找到的结果检索哪些字段。

没有尝试过,它应该看起来像这样:

err := collection.Find(nil).Select(bson.M{"categories": bson.M{"$elemMatch": bson.M{"slug": "general"}}}).One(&result)

为了获得嵌套的 Category 对象,您可以让result成为一个包含结构:

type CategoryContainer struct {
    Categories []Category{} // Or even [1]Category{} in this case
}

然后简单地获取类别:

category := result.Categories[0]

3.

对于更新子文档,已经有很好的帖子,例如:

MongoDB:更新子文档

于 2014-03-20T03:27:31.680 回答
0

虽然(如上所述)这有效:

错误 := collection.Find(nil).Select(bson.M{"categories": bson.M{"$elemMatch": bson.M{"slug": "general"}}}).One(&result)

但我认为这将查询集合中的所有文档,然后选择正确的文档。因此,我认为下面的解决方案更有效。

错误 := collection.Find(bson.M{"categories": bson.M{"$elemMatch": bson.M{"slug": "general"}}}).One(&result)
于 2017-07-02T11:27:37.110 回答