简体   繁体   English

非关系型数据库设计的困惑

[英]Non-relational database design confusion

I am learning MongoDB and trying to get my head round the concept of not using a relational database schema.我正在学习 MongoDB 并试图理解不使用关系数据库模式的概念。

For my application I want to be able to add new Users and Projects and assign Users to Projects by Roles eg 'manager', 'developer' etc. Users should be able to view details about the Projects in which they have a Role.对于我的应用程序,我希望能够添加新用户和项目并按角色将用户分配给项目,例如“经理”、“开发人员”等。用户应该能够查看有关他们拥有角色的项目的详细信息。 Retrieving details about a Project should include a list of Users also with Roles in that Project.检索有关项目的详细信息应包括在该项目中也具有角色的用户列表。

Supposing I add a new User via a POST to /users .假设我通过 POST 添加一个新用户到/users I can add a new Project with a POST to /projects .我可以将一个带有 POST 的新项目添加到/projects

Then I want to give the User a Role in the Project.然后我想在项目中给用户一个角色。 I could do a PUT to /users/{id} with the Role (including Role name and Project Id) but then retrieving details of a Project via a GET /projects/{id} won't include the list of Users with Roles in that Project.我可以使用角色(包括角色名称和项目 ID)对/users/{id}执行 PUT,但是通过 GET /projects/{id}检索项目的详细信息将不包括具有角色的用户列表那个项目。

I could have instead POSTed the Roles to /project/{id} (including Role name and User Id) but then a GET /users/{id} won't include their Roles and I might want to display the Roles a User is involved in on their landing page.我可以改为将角色发布到/project/{id} (包括角色名称和用户 ID),但是 GET /users/{id}将不包括他们的角色,我可能想显示用户参与的角色在他们的着陆页上。

To create a new Role would I really have to PUT it to /users/{id} and then do another PUT to /projects/{id} with the Role and the User?要创建一个新角色,我真的必须将它放到/users/{id} ,然后再将角色和用户放到/projects/{id}吗?

The above could be achieved in a relational database by simply creating a Roles table which might include the Role name, User Id and Project Id.以上可以在关系数据库中通过简单地创建一个角色表来实现,该表可能包括角色名称、用户 ID 和项目 ID。 Then I could perform joins and access whichever information I needed.然后我可以执行连接并访问我需要的任何信息。

Am I missing something here?我在这里错过了什么吗? Any tips or useful references would be greatly appreciated.任何提示或有用的参考将不胜感激。

I'm afraid you have hit the big downside to NOSQL databases when coming from relational ones.当您来自关系数据库时,恐怕您已经遇到了 NOSQL 数据库的重大缺点。 The answer is to forget everything you know about normalisation and to duplicate the data in to both places and then keep them both up to date.答案是忘记关于规范化的所有知识,并将数据复制到两个地方,然后使它们都保持最新。

I know this goes against everything you would be taught in a SQL database but that is on of the trade offs.我知道这与您在 SQL 数据库中学习的所有内容背道而驰,但这是一种权衡。 Rob Volk did a good article on this here https://robvolk.com/nosql-design-patterns-for-relational-data-9c2c11ae3b4a Rob Volk 在这里做了一篇很好的文章https://robvolk.com/nosql-design-patterns-for-relational-data-9c2c11ae3b4a

This might seem bad at first but it actually makes sense because what you are basically doing is decreasing read time at the cost of increasing write time and extra memory.乍一看这似乎很糟糕,但实际上是有道理的,因为您所做的基本上是以增加写入时间和额外内存为代价来减少读取时间。 Memory is cheap these days and you are going to write user permissions a whole lot less than you are going to read them.现在内存很便宜,你写入的用户权限比读取它们的要少得多。

You are right.你是对的。 The NOSql databases has a concept that is not a problem if you repeat the same data along the "tables", since that the repeated information have a hash code, wich will identify a data, that you will use when you want update it. NOSql 数据库有一个概念,如果您沿着“表”重复相同的数据,这不是问题,因为重复的信息有一个哈希码,它将标识一个数据,当您想要更新它时将使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM