Leave a comment

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:

  1. restructure your code.
  2. implement some kind of locking in your code.
  3. 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



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

%d bloggers like this: