MongoDB Schema Design | Embedding or Referencing? MongoDB Tutorials

Neeraj Garg
2 May 202205:05

Summary

TLDRこのビデオスクリプトでは、組み込みデータモデルと正規化データモデルの概念について解説しています。組み込みデータモデルは、関連データを単一のドキュメントや構造に格納する方法で、1対1や1対多の関係を効率的に扱えることが特徴です。一方、正規化データモデルは、テーブル間の関係を参照によって表現し、より複雑な多対多の関係を管理するのに適しています。どちらのモデルを使用するかは、データの重複、クエリのパフォーマンス、アプリケーションレベルでのデータ整合性の管理など、複数の要因を考慮して決定されます。MongoDBの`$lookup`を用いたジョイン操作の例も紹介されており、データモデリングの選択肢について深く理解することができます。

Takeaways

  • 😀 Embedded data models store related data in a single document, called denormalized data
  • 😯 With normalized data models, references are used for relations between tables
  • 😀 Embedded models have better read performance and ability to retrieve related data in one operation
  • 🤔 Embedded models can lead to duplication of data
  • 😮 Normalized models allow more complex many-to-many relationships using $lookup
  • 😃 Embedded models are good for one-to-one and one-to-many relationships
  • 🤨 Application needs to handle joins and data integrity with normalized models
  • 😑 Embedded child documents in parent document for one-to-many relationships
  • 😐 Use embedded models for data that needs to be queried frequently
  • 🙂 Choose normalized models when embedding leads to high duplication

Q & A

  • 埋め込みデータモデルとは何ですか?

    -埋め込みデータモデルは、関連するデータを単一の構造またはドキュメント内に格納する方法です。これにより、複数のコレクションを作成または維持することなく、異なるエンティティ間の関係を管理できます。

  • 正規化データモデルとはどのようなものですか?

    -正規化データモデルは、テーブル間の関係を参照によって管理する方法です。NoSQLデータベースでは、コレクション間で参照整合性制約を適用することはできません。そのため、アプリケーション側で結合やデータの整合性を管理する必要があります。

  • 埋め込みデータモデルを使用するシナリオは?

    -一対一または一対多の関係がある場合に埋め込みデータモデルを使用します。例えば、ユーザーが1つの電話番号と1つのメールアドレスを持っている場合、これらはユーザードキュメント内に埋め込まれます。

  • 正規化データモデルを使用するシナリオは?

    -データの重複が発生するか、より複雑な多対多の関係を表現する必要がある場合に正規化データモデルを使用します。例えば、ユーザードキュメントと連絡先ドキュメントが別々にあり、それらが相互に参照される場合です。

  • 埋め込みデータモデルの利点は何ですか?

    -読み取り操作のパフォーマンス向上、関連データを単一のデータベース操作で要求・取得できること、複数のコレクションからデータを取得するための複数操作が不要になること、関連データを単一のアトミック書き込み操作で更新できることが利点です。

  • 参照を使用するデータモデルの利点は何ですか?

    -データの重複を避け、多対多の関係をより適切に表現できることが利点です。また、MongoDBの$lookupを使用して、正規化形式で設計されたコレクション間で結合を行うことができます。

  • なぜ顧客を中心にデータを埋め込むのですか?

    -顧客情報は頻繁にクエリされるため、顧客を中心にデータを埋め込むことで、クエリのパフォーマンスを向上させることができます。例えば、eコマースの顧客が複数の住所を持つ場合、これらの住所を顧客ドキュメントに埋め込むことが推奨されます。

  • どのような場合に$lookupを使用しますか?

    -MongoDBの$lookupは、異なるコレクション間で結合を行うために使用されます。これは、正規化データモデルを使用している場合に特に有効で、関連するデータを複数のコレクションから取得する必要がある場合に使用します。

  • アトミック書き込み操作の重要性は何ですか?

    -アトミック書き込み操作により、データベース内の関連データが一貫性を保ち、一度に更新されることを保証します。これにより、データの整合性が向上し、エラーの可能性が減少します。

  • データモデルを選択する際の主な考慮点は何ですか?

    -データの関係性、クエリのパフォーマンス、データの整合性、アプリケーションの複雑さ、およびデータの重複の有無が、データモデルを選択する際の主な考慮点です。

Outlines

00:00

😊埋め込みデータモデルと正常化データモデルの違い

埋め込みモデルは関連データを1つに格納し、正常化モデルは参照を使って表現する。それぞれの特徴と適した使用場面がある。

Mindmap

Keywords

💡埋め込みデータモデル

単一のドキュメントや構造体に関連データを含めてデータを格納するデータモデルです。関係性のあるエンティティ間の複数のコレクションを作成・管理する必要がなく、ひとつのコレクションにほとんどの関係性が含まれます。

💡正規化データモデル

テーブル間の関係性を参照によって表現するデータモデルです。コレクションやテーブル間に参照整合性の制約を適用することができません。参照は正規化されたデータモデルで、テーブルが別のテーブルの主キーを外部キーとして参照する関係性です。

💡ドルアップ

MongoDBの構文で、コレクション間で結合を実行するために使用されます。正規化された形式で設計されたコレクションからデータを取得するのに役立ちます。

💡読み取り性能

埋め込みデータモデルは、関連データを1つのデータベース操作で要求および取得できるため、読み取り操作のパフォーマンスが向上します。

💡データの重複

埋め込みがデータの重複につながる場合は、参照を使用する正規化データモデルを利用する必要があります。

💡複雑な多対多の関係性

より複雑な多対多の関係性を表現するために、参照を使用する正規化データモデルを利用します。

💡アトミックライト

埋め込みデータモデルでは、関連データを1つのアトミックなライト操作で更新できます。

💡結合

正規化されたコレクションからドルアップを使用してデータを取得することで、コレクション間の結合ができます。

💡クエリ

埋め込みデータモデルでは、関連データを含むドキュメントをクエリすることで、効率的な読み取りができます。

💡整合性

正規化データモデルではコレクションやテーブル間に参照整合性の制約を適用できないため、アプリケーションレベルでデータの整合性を維持する必要があります。

Highlights

Embedded data model stores related data in a single document instead of multiple collections

Normalized data models use references between tables instead of embedding data

Applications must handle joins and data integrity with normalized models

Use embedded for 1:1 and 1:many relationships by embedding child in parent document

Embed addresses in customer document instead of separate collections

Embedding improves read performance and allows retrieving related data in one operation

Use referencing when embedding leads to duplication of data

Referencing helpful for more complex many-to-many relationships

Can use $lookup to join normalized collections instead of embedding

Embedded benefits: better performance, single retrieval operation

Update related embedded data atomically in one write operation

Referencing helpful when embedding causes data duplication

Many-to-many relationships better with referencing

$lookup to join normalized collections instead of embedding

Can fetch related data from normalized collections with $lookup

Transcripts

play00:00

[Music]

play00:06

what are embedded data model embedded

play00:08

related data in a single structure or

play00:11

document we want to

play00:14

store our data in a single

play00:16

document in json single document

play00:20

also called denormalized

play00:22

we are not creating or maintaining

play00:25

multiple collections to maintain

play00:27

relationships amongst different entities

play00:30

right so

play00:31

it is one

play00:33

collection which is having data of

play00:36

all the relations

play00:37

or most of the relations in that

play00:40

particular

play00:41

collection in the form of embedded

play00:43

documents

play00:46

normalized data models

play00:48

the second form

play00:50

it uses references for the relations

play00:52

among tables

play00:55

now

play00:56

with this approach if we follow

play00:58

normalized data models in

play01:01

almost all the nosql databases

play01:05

there are no

play01:08

referential constraints or integrity

play01:10

constraints which have

play01:12

been there

play01:14

which can be applied across collections

play01:17

or across tables so in this case when we

play01:20

are talking about references

play01:22

it is more or less

play01:24

like normalized data model which is

play01:27

one table

play01:28

is referring

play01:30

the

play01:30

data in another table which is primary

play01:32

key foreign key kind of relationships

play01:34

right so in that case if we go with this

play01:37

approach

play01:38

application must take care of joins and

play01:40

integrity of the data we will have to

play01:43

take care of

play01:44

all those things at the application

play01:46

level

play01:48

right now question is which one to use

play01:51

when

play01:52

right

play01:53

let me talk about that also

play01:57

embedded data models when to use when

play02:00

there is relationship

play02:02

one to one or one two many relationships

play02:05

are there

play02:06

on the right hand side of the screen you

play02:09

can see that

play02:10

this is

play02:11

embedded model right we have

play02:13

user and we are supposing that one user

play02:17

will have one phone and one email right

play02:20

and

play02:20

access also we can have

play02:23

controlled user will have one level of

play02:26

or

play02:28

a user will have

play02:30

the access defined at a single level

play02:33

only user will not have multiple records

play02:35

of access we can manage those in a

play02:38

single document

play02:40

so that way we can

play02:43

use embedded data model

play02:46

for one to one embed the document in the

play02:48

document meant for query

play02:50

for example a student with one address

play02:54

address can be embedded into the main

play02:55

collection it should not be the other

play02:57

way

play02:58

what will happen in case of one to many

play03:00

again embed the child document in the

play03:02

parent document for example an

play03:04

e-commerce customer with multiple

play03:06

addresses

play03:07

addresses will be a part of main

play03:09

collection which is a customer

play03:10

collection it should not be that we have

play03:13

address collection and there we are

play03:14

embedding customer as a part of because

play03:17

customer

play03:18

is going to be queried most of the time

play03:20

we will not query on addresses we will

play03:22

query on the customer side

play03:26

embedding benefits

play03:28

it will lead to better performances

play03:30

for read operations

play03:33

ability to request and retrieve related

play03:36

data in a single database operation

play03:38

we will not need to have multiple

play03:40

operations to retrieve data from

play03:42

multiple collections

play03:46

make it possible to update related data

play03:48

on a single atomic write operation as

play03:50

well

play03:51

referencing

play03:53

when to use it

play03:55

when embedding

play03:57

leads in duplication of data

play04:00

so there could be the possibility that

play04:02

when you are going to embed the data so

play04:05

it will lead to duplication of data

play04:09

to represent more complex many-to-many

play04:12

relationships

play04:15

use dollar lookup to join such

play04:17

collections

play04:20

dollar lookup is a mongodb construct

play04:22

which can be used to perform joins

play04:24

across collections

play04:26

this will help

play04:29

to

play04:30

fetch data from the collections where

play04:33

the data is designed in the normalized

play04:35

form

play04:37

so if you see on the right hand side

play04:39

this is an example where

play04:41

we have a user document and we have a

play04:43

contact document

play04:45

and

play04:46

access document

play04:48

so if you see user id is referred

play04:51

inside contact document and access

play04:53

document also so

play04:54

if we want to fetch data we can use

play04:57

dollar lookup

play04:59

[Music]

Rate This

5.0 / 5 (0 votes)

Do you need a summary in English?