通过Mongoid/MongoDB进行多对多建模 [英] Modeling many-to-many :through with Mongoid/MongoDB
问题描述
我对Mongoid/MongoDB相对较新,我对如何建模特定的多对多关系有疑问.
I'm relatively new to Mongoid/MongoDB and I have a question about how to model a specific many-to-many relationship.
我有一个User
模型和一个Project
模型.用户可以属于多个项目,并且每个项目成员资格都包含一个角色(例如,管理员",编辑者"或查看者").如果我使用的是ActiveRecord,则可以使用has_many :through
在User
和Project
之间建立多对多关联,然后在联接表中放置一个角色字段.
I have a User
model and a Project
model. Users can belong to many projects, and each project membership includes one role (eg. "administrator", "editor", or "viewer"). If I were using ActiveRecord then I'd set up a many-to-many association between User
and Project
using has_many :through
and then I'd put a field for role in the join table.
在MongoDB中对此场景进行建模的好方法是什么,我如何用Mongoid声明该模型?下面的示例似乎是对此建模的一种好方法,但是我不知道如何用Mongoid优雅地声明User
与嵌入的ProjectMembership
之间的关系.
What is a good way to model this scenario in MongoDB and how would I declare that model with Mongoid? The example below seems like a good way to model this, but I don't know how to elegantly declare the relational association between User
and the embedded ProjectMembership
with Mongoid.
提前谢谢!
db.projects.insert(
{
"name" : "Test project",
"memberships" : [
{
"user_id" : ObjectId("4d730fcfcedc351d67000002"),
"role" : "administrator"
},
{
"role" : "editor",
"user_id" : ObjectId("4d731fe3cedc351fa7000002")
}
]
}
)
db.projects.ensureIndex({"memberships.user_id": 1})
推荐答案
对一个好的Mongodb模式进行建模实际上取决于您如何访问数据.在上述情况下,您将为您的memberships.user_id索引编制索引,这看起来不错.但是,随着您添加查看器,编辑器和管理员,您的文档大小也会增加.另外,您的架构将使查询这样的查询变得困难:
Modeling a good Mongodb schema really depends on how you access your data. In your described case, you will index your memberships.user_id key which seems ok. But your document size will grow as you will add viewers, editors and administrators. Also, your schema will make it difficult to make querys like:
查询项目,其中user_id xxx是编辑者:
Query projects, where user_id xxx is editor:
同样,您可能不需要查询这样的项目,因此您的架构看起来不错.但是,如果您需要通过user_id AND角色查询项目,我建议您创建一个'project_membership'集合:
Again, you maybe do not need to query projects like this, so your schema looks fine. But if you need to query your projects by user_id AND role, i would recommend you creating a 'project_membership' collection :
db.project_memberships.insert(
{
"project_id" : ObjectId("4d730fcfcedc351d67000032"),
"editors" : [
ObjectId("4d730fcfcedc351d67000002"),
ObjectId("4d730fcfcedc351d67000004")
],
"viewers" : [
ObjectId("4d730fcfcedc351d67000002"),
ObjectId("4d730fcfcedc351d67000004"),
ObjectId("4d730fcfcedc351d67000001"),
ObjectId("4d730fcfcedc351d67000005")
],
"administrator" : [
ObjectId("4d730fcfcedc351d67000011"),
ObjectId("4d730fcfcedc351d67000012")
]
}
)
db.project_memberships.ensureIndex({"editors": 1})
db.project_memberships.ensureIndex({"viewers": 1})
db.project_memberships.ensureIndex({"administrator": 1})
甚至更容易...在您的项目模式上添加索引:
Or even easier... add an index on your project schema:
db.projects.ensureIndex({"memberships.role": 1})
这篇关于通过Mongoid/MongoDB进行多对多建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!