MongoDB Schema Design | Embedding or Referencing? MongoDB Tutorials
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
😊埋め込みデータモデルと正常化データモデルの違い
埋め込みモデルは関連データを1つに格納し、正常化モデルは参照を使って表現する。それぞれの特徴と適した使用場面がある。
Mindmap
Keywords
💡埋め込みデータモデル
💡正規化データモデル
💡ドルアップ
💡読み取り性能
💡データの重複
💡複雑な多対多の関係性
💡アトミックライト
💡結合
💡クエリ
💡整合性
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
[Music]
what are embedded data model embedded
related data in a single structure or
document we want to
store our data in a single
document in json single document
also called denormalized
we are not creating or maintaining
multiple collections to maintain
relationships amongst different entities
right so
it is one
collection which is having data of
all the relations
or most of the relations in that
particular
collection in the form of embedded
documents
normalized data models
the second form
it uses references for the relations
among tables
now
with this approach if we follow
normalized data models in
almost all the nosql databases
there are no
referential constraints or integrity
constraints which have
been there
which can be applied across collections
or across tables so in this case when we
are talking about references
it is more or less
like normalized data model which is
one table
is referring
the
data in another table which is primary
key foreign key kind of relationships
right so in that case if we go with this
approach
application must take care of joins and
integrity of the data we will have to
take care of
all those things at the application
level
right now question is which one to use
when
right
let me talk about that also
embedded data models when to use when
there is relationship
one to one or one two many relationships
are there
on the right hand side of the screen you
can see that
this is
embedded model right we have
user and we are supposing that one user
will have one phone and one email right
and
access also we can have
controlled user will have one level of
or
a user will have
the access defined at a single level
only user will not have multiple records
of access we can manage those in a
single document
so that way we can
use embedded data model
for one to one embed the document in the
document meant for query
for example a student with one address
address can be embedded into the main
collection it should not be the other
way
what will happen in case of one to many
again embed the child document in the
parent document for example an
e-commerce customer with multiple
addresses
addresses will be a part of main
collection which is a customer
collection it should not be that we have
address collection and there we are
embedding customer as a part of because
customer
is going to be queried most of the time
we will not query on addresses we will
query on the customer side
embedding benefits
it will lead to better performances
for read operations
ability to request and retrieve related
data in a single database operation
we will not need to have multiple
operations to retrieve data from
multiple collections
make it possible to update related data
on a single atomic write operation as
well
referencing
when to use it
when embedding
leads in duplication of data
so there could be the possibility that
when you are going to embed the data so
it will lead to duplication of data
to represent more complex many-to-many
relationships
use dollar lookup to join such
collections
dollar lookup is a mongodb construct
which can be used to perform joins
across collections
this will help
to
fetch data from the collections where
the data is designed in the normalized
form
so if you see on the right hand side
this is an example where
we have a user document and we have a
contact document
and
access document
so if you see user id is referred
inside contact document and access
document also so
if we want to fetch data we can use
dollar lookup
[Music]
Browse More Related Video
基本情報技術者試験対策 70 データベース(正規化)
【エクセル・Excel】Lesson23:文字列操作関数(LEFT関数 RIGHT関数 MID関数 FIND関数)×VLOOKUP関数(ユースフル リスキリング習慣化講座)【研修・eラーニング】
畳み込みの仕組み | Convolution
You MUST KNOW These Traits in Rust
This tool is a MUST for programmers 👩💻 #coder #technology #developer #software #tech #linux
【Python入門 #6】辞書と集合 | 複数の変数を上手く扱えるようになります
5.0 / 5 (0 votes)