MongoDB – Things to Keep in Mind

Hello everyone,

In this blog post I will try to keep things that I have learned over time when working with MongoDB.

  1. When deciding on data structure to use(or Schema), Keep in mind its use cases for eg. if it will be used for searching than try to have proper key values rather than having dynamic object property.
    Let me give an example.
// Don't do like this
{
name: "Shoes",
description: "Awesome shoes",
features: {
      size: 'L',
      color: 'Red',
      type: 'Party'
      weight: '350',
  }
...
}

// Do like this
{
name: "Shoes",
description: "Awesome shoes",
features: [
      {key:'size', value: 'L'},
      {key:'color', value: 'Red'},
      {key:'type', value: 'Party'},
      {key:'weight', value: '350'}
  ]
...
}

With Above 2 data structure, it becomes easy to add index on features and do the searching.

2. After prototyping, Try to delete early data in collection as it might create confusion when we look into collection’s sample document(In Compass), it will show documents from start which we may not represent the actual data structure thats getting finally used in application. Maybe we have removed/added some fields.

3. When running aggregation pipeline, try to run explain query on it and see if proper index are getting used or not and whether it requires new indexes.

4. MongoDB === Document Store

Leave a Reply