Design Better Database Models

Learn how to design better models when using MongoDB as a database in your application.

Below are some key points to design better database models:

  1. We always use the correct data type for the field. For example, numbers should not be stored as a string, they should be of int, long, decimal, or double type only.
  2. Field names should have a consistent naming approach. For example, they should either be in camelCase or use an underscore (_) between two or more words.
  3. MongoDB offers great flexibility to design models using embedded models. If possible, we should always convert the information of an entity into objects and embed it into the primary model. As a result, the information representation becomes very good.
  4. Normalization should be considered when we don’t have a good benefit of embedding. In that case, the embedded document is used in multiple collections.
  5. We keep our collection schema consistent for all the records. We don’t skip fields in records and avoid additional handling on the query level.
  6. Indexing is important. We find out which fields need indexing and apply it to them. We should avoid unnecessary indexing because it may cause performance issues on write operations.
  7. We use GridFS to store large files instead of binary Data.
  8. We should prefer the Date type for dates instead of storing them into numbers.
  9. Fields like a Birthdate should be of type Date, and should not store any time value.
  10. We store the currency, and use the Amount field to store the lowest value of the currency.

Get hands-on with 1200+ tech skills courses.