理解MongoMappers的诸多关系

发布于 2024-10-31 07:58:15 字数 1613 浏览 1 评论 0原文

好吧,首先我对 Ruby、Rails、Mongo 甚至 ActiveRecord 不是很了解,所以如果这些问题相当基本,我深表歉意。首先,我有一个“事件”模型,并且我在“评论”模型中添加了许多关系。现在,我对 mongo(或任何文档数据库)的(基本)理解是,不建议进行外键查找(或在 mongo 中调用的任何内容),而是选择在单个文档中存储尽可能多的内容。考虑到这一点,我希望有一个事件集合,其中每个文档中嵌入注释 JSON/BSON,即

event:

{
 Title: "some event"
 Comments : [
    { user_id: [object id], comment: "comment"}, 
    { user_id: [object id], comment: "other comment"}
 ]
}

相反,我看到已创建注释集合以及事件集合。这是映射这种关系的正确方法吗?如果是,我该如何停止创建评论集合,而是将信息直接嵌入到事件中?我的猜测是,我必须告诉我的 Comment 映射不要有 _id 属性,因此不属于它自己的集合。


编辑:我发现我在第一个问题中寻找的是使用 MongoMapper::EmbeddedDocument 而不是包含 MongoMapper::Document


其次,我希望用户能够将自己标记为“参加”或只是“感兴趣”一个事件,我认为这将被建模为事件上的引用 user_id 的数组。所以我最初认为它会被映射为两个“多”关系,但据我所知,我传递给许多方法的常量名称用于创建 getter/setter,这对我来说不起作用如果我有两个相同类型的关系。

换句话说,如果我有这个映射:

class Event
   many :users
end

那么我的理解是,我将为 users 属性生成 getter 和 setter。所以我可以做类似的事情:

event.users << someAttendingUser

这一切都很好,此时我希望希望存储对用户的引用,而不是整个用户 BSON(与上面的注释示例不同)。现在的问题是,当我有两个到用户集合的映射时,就像我需要“参加”和“感兴趣”用户一样,我该如何做到这一点?

总结一下这个漫无目的的内容:

  • 有时我想将许多关系直接存储为原始文档中的 BSON,而不是作为对另一个集合中文档的引用。我该如何做到这一点?

  • 有时我想在文档上存储许多相同类型的关系,并且我确实希望它们成为对另一个集合中文档的引用。我该如何做到这一点?

希望这是有道理的,如果我在这里问显而易见的问题,我深表歉意。


编辑2:

好的,好吧,在问这个问题之前我确实进行了搜索,但看来我现在已经找到了我的两个问题的答案,所以我会自己结束这个问题。

第二部分要求我为映射指定一个类名选项,即

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end

Ok, so firstly Im not very in the know when it comes to Ruby, Rails, Mongo or even ActiveRecord so I apologise if these questions are fairly basic. Firstly, I have an "Event" model and I have added a many relationship to my "Comment" model. Now it is my (basic) understanding of mongo (or any document db) that foreign key lookups (or whatever they are called in mongo) are not advised, instead opting for storing as much as possible in a single document. With this in mind I would expect to have a single Events collection with the Comment JSON/BSON embedded in each document i.e.

event:

{
 Title: "some event"
 Comments : [
    { user_id: [object id], comment: "comment"}, 
    { user_id: [object id], comment: "other comment"}
 ]
}

Instead I am seeing that a comments collection has been created as well as the event collection. Would this be the correct way to map this relationship, if so how do I stop the comment collection been created and instead have the info embedded directly into the event? My guess is that I have to tell my Comment mapping not to have a _id property and thus not belong to its own collection.


EDIT: I found out that what I was looking for here in my first question was to use MongoMapper::EmbeddedDocument instead of including MongoMapper::Document


Secondly, I want users to be able to flag themselves as "attending" or just "interested" an event, which I assumed would be modelled as an array of reference user_id's on the event. So I initially thought it would be mapped as two "many" relationships, but as far as I can tell the name of the constant I am passing to the many method is used to create the getter/setters, which wouldn't work for me if I had two many relationships for the same type.

In other words, if I have this mapping:

class Event
   many :users
end

Then it's my understanding that I will then have getters and setters generated for the users property. So I can do something like:

event.users << someAttendingUser

This is all good, and at this point in time I would want the reference to the user to be stored and not the whole user BSON (unlike with the comments example above). Now the problem is how do I do this when I have two many mappings to the user collection, as with my need for both "attending" and "interested" users?

So to summarise this rambling:

  • Sometimes I want to store many relationships directly as BSON in the orinal document, and not as a reference to a document in another collection. How do I do this?

  • Sometimes I want to store many relationships of the same type on a document and I DO want them to be references to a document in another collection. How do I do this?

Hope this made sense, and I apologise if Im asking the obvious here.


EDIT 2:

Ok, well I really did search before asking this question, but it appears that I have now found the answer to both of my problems so I will close the question myself.

The second part required me to specify a class name option for the mapping i.e.

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-11-07 07:58:15

好吧,好吧,在问这个问题之前我确实进行了搜索,但看来我现在已经找到了我的两个问题的答案,所以我会自己结束这个问题。

第二部分要求我为映射指定一个类名选项,即

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end

Ok, well I really did search before asking this question, but it appears that I have now found the answer to both of my problems so I will close the question myself.

The second part required me to specify a class name option for the mapping i.e.

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文