MongoDB – Schema Design

What’s the single most important factor in designing your application schema within MongoDB?
Matching the data access patterns of your application.

Few Points:
1. Rich Documents
2. Pre Join / Embed Data (Required joins should be stored along with Document)
3. No Mongo Joins (We need to do in Application level)
4. No Constraints
5. Atomic operations (Only in one document)
6. No Declared schema (All documents will follow same schema, with minor exceptions. i.e we can add few more required attributes)

——–
Normalization of Database

Normalization is used for mainly two purpose,

Eliminating redundant(useless) data.
Ensuring data dependencies make sense i.e data is logically stored.

http://www.studytonight.com/dbms/database-normalization.php

———-
Normalization, JDBC, DAO, Queries…all are making it complex to design RDBMS schema and developing application.
In today’s world, space is available at low cost.
Storing data in a denormalized format.
These things triggered Schema-less, Denormalized systems.
——–

MongoDB Transactions
No Support for Transactions
Supports ACID for single document
1. Design to fit all in one document
2. Implement in Software
3. Tolerate issues
———————-
One to One Relations

What’s a good reason you might want to keep two documents that are related to each other one-to-one in separate collections? Check all that apply.
1. To reduce working set size of application
2. Because the combined size of documents is more than 16MB

———————-
One to Many Relations

When is it recommended to represent a one to many relationship in multiple collections?
Whenever the many is large.
———————-
Many to Many relations:

Books with Authors: Both should be first class collections with reference to other object.
Students and Teachers: same as above

————————-

MultiKey Indexes

Improves efficiency in searching

Reference:
https://docs.mongodb.com/manual/core/index-multikey/
To index a field that holds an array value, MongoDB creates an index key for each element in the array. These multikey indexes support efficient queries against array fields. Multikey indexes can be constructed over arrays that hold both scalar values (e.g. strings, numbers) and nested documents.

also study .explain()
This shows how find is working smoothly with the multikey search.
———————–
Benefits of embedding
1. Improved read performance
2. One round trip to the DB.
————————
Trees:

https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-ancestors-array/

————————
Denormalized Data

1:1 – Embed
1:Many – Embed
Many:Many – Link as arrays

—————————

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s