October 28, 2016 by oneOokay
Goals of normalization
- free the database of modification anomalies
- minimize redesign when extending
- avoid bias toward any particular access pattern
MongoDB: embed data as you can, pre-join data as you can.
relational databases: transactions ensure you ACID
Mongo db can provide atomic operations:
- restructure your code.
- implement some kind of locking in your code.
- tolerate (a little bit of inconsistency)
These mongo db operations operate atomically within a single document: Update, findAndModify, $addToSet(within an update) and $push within an update. Atomic operation on a single document
1 document’s maximum size: 16 MB
1 to 1 relations: some good reasons you might want to keep two documents that are related to each other one-to-one in separate collections:
- to reduce the working set size of your application
- the combined size of the documents would be larger than 16 MB
1 to many relationships: people and city. Only this is recommended to have separate documents. True linking: two documents: city has city as its id, people has the city.
1 to Few: probably a single document and just embed them.
Many to many:
Few to Few: use the rich document hierarchy of MongoDB
Benefits of Embedding:
- Improved Read performance
- one round trip to the db. the first bite read will be slow but the following bites will be really fast
Representing Trees: put ancestors in an array or put children in children array.
When to denormalize:
- 1:1 -> Embed
- 1:many -> Embed (from the many to the one)
- many: many -> link
GRIDFS: used to store large files such as videos. break up large files in chunks