MongoDB Schema Design By Examples

This is a (super) late post, it should be posted back on October 2014. This slide is the material of presentation on NoSQL Indonesia October Meetup 2014. I was invited to be a speaker in a such short notice (one day before), so pardon for the slides if it is far from good.

Well, here it is. Hope this helps you to give better understanding on different schema design on different cases. Thanks that included a lot on this slide.


  • 1. MongoDB Schema Design by Examples October th MeetUp Oct,25 2014
  • 2. MongoDB Overview Document Database Documents (objects) map nicely to programming language data types. Embedded documents and arrays reduce need for joins. Dynamic schema makes polymorphism easier. High Performance Embedding makes reads and writes fast. Indexes can include keys from embedded documents and arrays. Optional streaming writes (no acknowledgments). High Availability Replicated servers with automatic master failover. Easy Scalability Automatic sharding distributes collection data across machines. Eventually-consistent reads can be distributed over replicated servers. Advanced Operations With MongoDB Management Service (MMS) MongoDB supports a complete backup solution and full deployment monitoring.
  • 3. Schema Design Differences RDBMS MongoDB Rows in one tables have the same structures Documents in each collections can have different structures Support joins and transactions No joins and No transactions Query : SQL Query : No SQL, speaks javascript Application doesn’t matters It is ALWAYS about your application
  • 4. MongoDB Schema Design Things to always have in mind when doing schema design Functionality to be supported (application side) Application’s access patterns (read and write ratio, query type, etc) Data Life Cycle (insert, update, delete) Schema Design Work flow Design / Develop Deployment Iterative Modification
  • 5. Schema Design Tips “Schema-less” doesn’t mean that you don’t need to design your schema KNOW and UNDERSTAND you application inside out There is no wrong answer to any schema design. If its not fit with your application then it will be a recipe of disaster eg : difficult queries to write and optimize, low performace app
  • 6. Use Case (1) use MongoDB in a lot of aspects Forum system Logs system Product specifications Search and filter products Crawler reports etc * is a Price comparison website focuses on gadget and electronics. Located in Jakarta, Indonesia
  • 7. Use Case (2) Pricebook Forum Here is forum thread document… Where is the comments data?
  • 8. Use Case (3) Pricebook Forum Comments data separated from forum thread collection. Why not using embedded docs? What if the forum thread getting more comments and grows bigger and bigger? Btw, MongoDB’s document size limit is 16MB
  • 9. Use Case (4) Pricebook Product Specification Pricebook has 12+ product categories Each of product has their own specifications, some are totally different. Eg : Smartphone, Television. Now imagine creating this schema design in MySQL or other DBMS? How many tables will you prepare for this? How many joined tables will you use for getting one specification of one product? (__ ____!)
  • 10. Use Case (5) Pricebook Product SStporee acll tihfe iscpeac ttaibole inn one document. Why? – One single query to get or delete the spec data – Specification data is less often updated, probably one time only
  • 11. Use Case (6) Pricebook Log Log as many data as you can. Don’t forget the easy-query aspect too. Eg : Count annual product view with product_id : 20468 db.log.count( { “time_bucket.year” : “2014”, “product_id” : “20468” } );
  • 12. Use Case (7) Using MongoDB for their logs system Stores impressions, clicks, object cache data, etc Hundred millions of documents * is an Ad Network that focuses on Interstitial Ads. Established in 2011 and located in downtown Toronto, Canada.
  • 13. Other (1) Tools Rockmongo, web-based admin for MongoDB ( MMS, MongoDB Monitoring System (
  • 14. Other (2) MMS, MongoDB Monitoring System
  • 15. Thank you! 🙂 •@hadiariawan •hadi.ariawan •

1 comment / Add your comment below

Leave a Reply

Your email address will not be published. Required fields are marked *