关系型数据 mongodb_MongoDB中的数据关系

关系型数据 mongodb

Relationships in MongoDB are used to specify how one or more documents are related to each other. In MongoDB, the relationships can be modelled either by Embedded way or by using the Reference approach. These relationships can be of the following forms:

MongoDB中的关系用于指定一个或多个文档之间的相互关系。 在MongoDB中,可以通过嵌入式方式或使用引用方法对关系进行建模。 这些关系可以具有以下形式:

  • One to One

    一对一

  • One to Many

    一对多

  • Many to Many

    多对多

MongoDB:嵌入式关系 (MongoDB: Embedded Relationship)

In this approach, one document will be embedded in another document (like a subset).

通过这种方法,一个文档将被嵌入到另一个文档中(例如子集)。

MongoDB:参考关系 (MongoDB: Reference Relationship)

In this approach, the documents are maintained separately (student and address). But, the student document will have a reference to the address document id field. This is basically used for designing normalized relationships. The basic idea behind any NoSQL database is to eliminate the complex relationships between documents, and MongoDB does the same. But, there are scenarios where these relationships are required to be modelled in the database.

用这种方法,文件分别保存(学生和地址)。 但是,学生文档将引用地址文档ID字段。 这基本上用于设计规范化的关系。 NoSQL数据库背后的基本思想是消除文档之间的复杂关系,而MongoDB也是这样做的。 但是,在某些情况下,需要在数据库中对这些关系进行建模。

MongoDB:一对一关系 (MongoDB: One to One Relationship)

Consider a student database of a college, where the college restricts only one course can be availed by the student at a time. So, this is nothing but One to One relationship i.e. a student can be enrolled only to one course at a time. Let us see how this can be modelled.

考虑一所大学的学生数据库,该大学限制学生一次只能选择一门课程 。 因此,这不过是一对一的关系,即一个学生一次只能注册一门课程。 让我们看看如何建模。

One to One Relationship in MongoDB documents

The above figure shows that there is student data which has a relationship with the course data. In MongoDB, these 2 collections can be embedded into one as shown below:

如上图所示,有具有与过程数据的关系的学生数据。 在MongoDB中,这两个集合可以嵌入到一个集合中,如下所示:

One to One Relationship in MongoDB documents

MongoDB:一对多关系 (MongoDB: One to Many Relationship)

Continuing with the same student database example, consider a student can have more than one address (permanent and current). Let us see how to model such a usecase which is a one to many relationship.

继续同一个学生数据库示例,请考虑一个学生可以有多个地址(永久地址和当前地址)。 让我们看看如何为这种具有一对多关系的用例建模。

One to Many Relationship in MongoDB documents

Above you can see the data to be modelled, and below we have modelled it for MongoDB storage.

您可以在上方看到要建模的数据,而在下方则可以为MongoDB存储建模。

One to Many Relationship in MongoDB documents

With this embedded approach, both the addresses of the student can be retrieved by a single query!

使用这种嵌入式方法,可以通过单个查询检索学生的两个地址!

With the reference approach, the above database can be modeled as shown below :

使用参考方法,可以对以上数据库进行建模,如下所示:

One to Many Relationship in MongoDB documents

Here we have used the ObejctIds of the addresses to reference it from the student document.

在这里,我们使用了地址的ObejctId来从学生文档中引用它。

MongoDB:多对多关系 (MongoDB: Many to Many Relationship)

Consider the same student database example, where a student can have different roles like leader, cricket captain, football captain etc. i.e. many roles can be accomplished by students or students can be associated to multiple roles. Let us see how to model this approach which is a many to many relationship.

考虑相同的学生数据库示例,其中学生可以扮演不同的角色,例如组长,板球队长,足球队长等。即,许多角色可以由学生完成,或者学生可以与多个角色相关联。 让我们看看如何对这种具有多对多关系的方法进行建模。

Embedded Approach:

嵌入式方法:

One to Many Relationship in MongoDB documents

Reference Approach:

参考方法:

One to Many Relationship in MongoDB documents

Note: In the above example, each role will be stored in a separate role document and the same is referred through their ObjectIds.

注意:在以上示例中,每个角色将存储在单独的role文档中,并且通过其ObjectId引用相同的role

翻译自: https://www.studytonight.com/mongodb/relationships-in-mongodb

关系型数据 mongodb

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值