简体   繁体   English

MongoDB中许多具有相同字段或一个数组的文档

[英]Many documents with the same field or one array in MongoDB

Let's say I have a system where users can vote for items that come with an identificator. 假设我有一个系统,用户可以在其中为标识符附带的项目投票。

Should I have a document for every user with an array of votes they had put in: 我是否应该为每位用户提供一个带有一系列投票的文档:

{user: A, votes: [x, y, z]}
{user: B, votes: [y, z]}
{user: C, votes: [x, z]}

or rather many documents for each vote: 或每次投票有很多文件:

{user: A, vote: x}, {user: A, vote: y}, {user: A, vote: z}
{user: B, vote: y}, {user: B, vote: z},
{user: C, vote: x}, {user: C, vote: z}

I feel like the second design is better but I might be wrong since I've only had experience with MySQL before. 我觉得第二个设计更好,但是我可能是错的,因为我以前只有MySQL的经验。

According to me first design is better than that of second . 根据我的说法,第一种设计比第二种更好。 Because you keep a document for a particular user . 因为您保留了特定用户的文档。 But in second case you are creating multiple document for a same user . 但是在第二种情况下,您将为同一用户创建多个文档。 Or you can create a document for particular votes ie who all are voted for x or y .... . 或者,您可以为特定的投票创建一个文档,即为xy投票的人。 Like 喜欢

   {votes: X, users: [A,C]}
   {votes: Y, users: [A, B]}
   {votes: Z, users: [A, B,C]}

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

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