简体   繁体   English

我们什么时候应该在 MongoDB 中使用嵌入式文档?

[英]When should we use embedded documents in MongoDB?

I read a lot about embedding in MongoDB, but still I don't know when to use it.我读了很多关于嵌入 MongoDB 的文章,但我仍然不知道何时使用它。 I thought of some scenarios:我想到了一些场景:

I have A collection named UserGroups with these fields:我有一个名为 UserGroups 的集合,其中包含以下字段:

  • _id _ID
  • name姓名

Now I want to add an user to Users collection:现在我想向用户集合添加一个用户:

  1. I should have a groupName field in Users collection and update it when UserGroups collection gets an update.我应该在 Users 集合中有一个groupName字段,并在 UserGroups 集合获得更新时对其进行更新。

  2. I should add the groupId field in Users collection and perform a join whenever I want the UserGroups.name.我应该在用户集合中添加groupId字段,并在需要 UserGroups.name 时执行连接。

  3. I should add the user document as an embedded document to UserGroups collection.我应该将用户文档作为嵌入文档添加到 UserGroups 集合中。

  4. Or I should add the userGroup document as an embedded document in Users collection.或者我应该将 userGroup 文档添加为用户集合中的嵌入文档。 though, I don't think I should do it.不过,我认为我不应该这样做。

Which option should I use?我应该使用哪个选项?

Due to MongoDB schema flexibility, developers no longer have to design the database schema first ieEntity Relationship Diagram .由于 MongoDB 模式的灵活性,开发人员不再需要首先设计数据库模式,即实体关系图 The design of the application comes first, then design the database schema to match the application usage (query, writes, updates, etc).首先是应用程序的设计,然后设计数据库模式以匹配应用程序的使用(查询、写入、更新等)。 See also Data Modelling .另请参阅数据建模

This is the reason why there is no right/wrong answer on the database schema design.这就是为什么数据库模式设计没有正确/错误答案的原因。 There are various use case for MongoDB, and different applications can leverage different models. MongoDB 有多种用例,不同的应用程序可以利用不同的模型。

When should we use embedded documents in MongoDB?我们什么时候应该在 MongoDB 中使用嵌入式文档?

Without knowing exactly how your applications will interact with the data, the following answer is only a general guidelines or approach :在不确切知道您的应用程序将如何与数据交互的情况下,以下答案只是一般准则或方法

Favour the embedding, unless there is a reason not to.支持嵌入,除非有理由不这样做。

Try to denormalise UserGroup into User first.尝试首先将UserGroup非规范化为User For example:例如:

{ userid: 1001,
  name: "Reza Tayebi", 
  group: {id: 10,
          name: "Developer"
  }
}

When the relationship is one to few (not many, not unlimited).当关系是一对少数(不是很多,不是无限)时。

The above example is applicable, if a user can either be in one or few groups.如果用户可以在一个或几个组中,则上述示例适用。 If there are more than one but limited to under 5, you may introduce an array.如果有多个但限制在 5 个以下,您可以引入一个数组。 However if a user can be in 20+ or an unlimited number of groups, you should start considering separating groups into another collection.但是,如果用户可以在 20 多个或无限数量的组中,您应该开始考虑将组分成另一个集合。

When retrieval is likely to happen together.当检索可能一起发生时。

If the retrieval of UserGroups is always associated with a user.如果UserGroups的检索总是与一个用户相关联。 For example, either Query some users with their respective groups , or Query some users given a specific group(s) .例如, Query some users with their respective groups ,或Query some users given a specific group(s)

When updates are likely to happen at the same time.当更新可能同时发生时。

If it is likely that you would update Users and UserGroups at the same time.如果您可能会同时更新UsersUserGroups Although starting from MongoDB 4.0, you can use multi-documents transactions if Users and UserGroups are not embedded, a single document transaction would be more performant.虽然从UserGroups 4.0 开始,如果没有嵌入Users和用户组,您可以使用多文档事务,但单个文档事务会更高效。 See also Atomicity and Transactions .另请参阅原子性和事务

When the field is rarely updated.当字段很少更新时。

If the name of a group is rarely updated in the collection.如果集合中很少更新组的名称。 For example, if the group name Developers required to be updated to Web Developers for the entire Users collection often, then embedding UserGroups is not suitable.例如,如果需要经常将整个Users集合的组名Developers更新为Web Developers ,则嵌入UserGroups是不合适的。

Again, above are only general guidelines that you should consider before designing your schema, see also the following resources:同样,以上只是您在设计架构之前应考虑的一般准则,另请参阅以下资源:

UPDATE:更新:

A new resource: Building with Patterns: A Summary .新资源: 使用模式构建:总结 Also worth mentioning, for situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document Transactions .另外值得一提的是,对于需要对多个文档(在单个或多个集合中)进行原子性读写的情况,MongoDB 支持多文档事务

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

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