Skip to content

Instantly share code, notes, and snippets.

@izumskee
Last active January 9, 2018 13:43
Show Gist options
  • Save izumskee/f6ed15c7a1720998edac66047beabc07 to your computer and use it in GitHub Desktop.
Save izumskee/f6ed15c7a1720998edac66047beabc07 to your computer and use it in GitHub Desktop.
MongoDB Rules of Schema design
  1. favor embedding unless there is a compelling reason not to

  2. needing to access an object on its own is a compelling reason not to embed it

  3. Arrays should not grow without bound. If there are more than a couple of hundred documents on the “many” side, don’t embed them; if there are more than a few thousand documents on the “many” side, don’t use an array of ObjectID references. High-cardinality arrays are a compelling reason not to embed.

  4. Don’t be afraid of application-level joins: if you index correctly and use the projection specifier (as shown in part 2) then application-level joins are barely more expensive than server-side joins in a relational database.

  5. Consider the write/read ratio when denormalizing. A field that will mostly be read and only seldom updated is a good candidate for denormalization: if you denormalize a field that is updated frequently then the extra work of finding and updating all the instances is likely to overwhelm the savings that you get from denormalizing.

  6. As always with MongoDB, how you model your data depends – entirely – on your particular application’s data access patterns. You want to structure your data to match the ways that your application queries and updates it.

Reference - https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-3?_ga=2.229531164.1254809724.1515476824-1745314314.1515476824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment