MongoDB - 我的用户文档是否应该包含项目 ID 列表? [英] MongoDB - should my user document hold a list of project ids?
问题描述
我有 users
和 projects
的集合.每个项目都与一个用户完全相关.
I have collections of users
and projects
.
Every project is connected exactly to one user.
我的问题是:每个用户都应该持有项目 ID 列表吗?
My question is: should every user hold the list of project ids?
如果我想检索特定用户的所有项目,哪个选项更有效和最佳实践:
If i want to retrieve all the projects of a specific user, which option is more efficient and best practice:
- 在用户 ID 属性上创建项目集合的索引.不仅仅是查询用户 ID 属性.
- 在项目 id 属性上创建项目集合的索引.如果用户持有其项目 ID,则只需查询项目集合以获取这些特定 ID.
选择哪个选项?也许还有第三种更好的选择?第一个选项的优点是我在删除/添加项目时不需要更新用户文档中的项目列表.
Which option to choose? Maybe there is a third option that is better? The advantage of the first option, is that i don't need to update the list of project in the user document when deleting/adding projects.
谢谢!
推荐答案
每个项目都与一个用户完全相关.
Every project is connected exactly to one user.
一个user
可以有许多projects
(一个项目只与一个用户相关联).这是一对多关系.
A user
can have many projects
(and a project is associated with one user only). This is a one-to-many relationship.
我的问题是:每个用户都应该持有项目 ID 列表吗?
My question is: should every user hold the list of project ids?
每个用户
都应该存储他/她的projects
列表.例如:
Every user
should store the list of his/her projects
. For example:
user:
id: <some value>,
name: <some value>,
email: <some value>,
projects: [
{ projectId: <some value>, projectName: <...>, projectDescription: <....>, otherInfo: { fld1: <...>, fld2: <...>, etc. } },
{ projectId: <some value>, projectName: <...>, projectDescription: <....>, otherInfo: { fld1: <...>, fld2: <...>, etc. } },
...
]
请注意,每个 project
都是 projects
数组中的子文档(对象或嵌入文档).project
有其相关的详细信息,如 projectId
、projectName
等.
Note that each project
is a sub-document (object or embedded document) within the projects
array. A project
has its related details like, projectId
, projectName
, etc..
如果我想检索特定用户的所有项目,选项更有效和最佳实践:
If i want to retrieve all the projects of a specific user, which option is more efficient and best practice:
一个.在用户 id 属性上创建项目集合的索引.不仅仅是查询用户 ID 属性.
a. Create an index on projects collection on the user id property. than just query on user id property.
b.在项目 id 属性上创建项目集合的索引.比,如果用户持有其项目 ID,只需查询项目这些特定 ID 的集合.
b. Create an index on project collection on the project id property. than, if the user holds its project ids, just query projects collection for those specific ids.
我认为,应该只有一个集合称为user_projects
.假设:(i) user
可能有 0 到 100 个项目,并且 (ii) project
的详细信息不会太大.
I think, there should be only one collection called as user_projects
. Assuming that: (i) a user
may have 0 to 100 projects, and (ii) a project
's details are not too huge.
这是一种将 1 对 N 关系的多"方嵌入单"方的模型.这是一种推荐的方法,对数据进行反规范化.这具有高效和快速查询的优点.这简化了事务,因为写入(插入、更新和删除)将是原子的,只需对同一集合中的文档进行单一操作.
This is a model of embedding the 'many' side of the 1-to-N relationship into the 'one' side. This a recommended way, de-normalizing the data. This has the advantage of efficient and fast queries. This simplifies transactions, as writes (inserts, updates and deletes) are going to be atomic with single operation to a document within the same collection.
关于检索特定用户的所有项目:
About retrieving all projects for a specific user:
您将使用 user
id
或 name
(带有唯一索引)来检索文档,而且速度非常快询问.您可以在 projects
数组上创建索引(数组字段上的索引称为 Multikey Indexes) - 在项目的字段上.例如,projectId
或/和 projectName
上的索引是有意义的.
You will be using the user
id
or name
(with a unique index) to retrieve a document, and it will be very fast query. You can have index on the projects
array (indexes on array fields are called as Multikey Indexes) - on the project's fields. For example, index on projectId
or/and projectName
makes sense.
您可以获取用户的所有项目 - 这是一个使用 user
id
/name
的简单查询.查询 projection 允许显示与 project
相关的哪些信息.您可以使用 find
或 aggregate
方法来构建查询.您可以使用 projectId
或 projectName
为 user
查询特定的 project
.由于 user
和 project
字段都有索引,这将是一个高效的查询.
You can get all projects for a user - its a simple query using the user
id
/ name
. Query projection allows what information related to project
is displayed. You can use a find
or aggregate
method to build the query.
You can query a specific project
for a user
, using the projectId
or projectName
. Since there are indexes on user
and project
fields, this will be an efficient query.
所以,我的建议是有一个集合,user_projects
,其中包含 user
的信息和 projects
信息.
So, my recommendation is to have a single collection, user_projects
, with a user
's information and the projects
information embedded in it.
这篇关于MongoDB - 我的用户文档是否应该包含项目 ID 列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!