MongoDB-我的用户文档应包含项目ID的列表吗? [英] MongoDB - should my user document hold a list of project ids?

查看:60
本文介绍了MongoDB-我的用户文档应包含项目ID的列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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:

  1. 在用户ID属性的项目集合上创建索引.不仅仅是查询用户ID属性.
  2. 在项目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.

一个用户可以有多个 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?

每个用户应该存储他/她的项目的列表.例如:

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:

a.在用户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-to-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 向特定的 project 查询一个 user .由于在 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 的信息和 project 信息

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆