Skip to main content

MongoDB, explained.

Published 2021/11/17 9:30

You've heard about MongoDB as a better alternative to SQL databases. You've even heard the term "NoSQL" being thrown around.

So what is MongoDB anyway?

Let's explain.

Suppose you are tasked to build a web service, where you give it an ID and it gives you back details about a user. These details include their home address and a list of hobbies. It would give you back a JSON file like so:

{
    "id": 105,
    "firstName": "Emily",
    "lastName": "Matilda",
    "address": {
        "street": "101 High St",
        "city": "Thurgarton",
        "postCode": "NR11 6YD"
    },
    "hobbies": ["Running", "Painting"]
}

You obviously need to store this data somewhere. How would this work if you were to store this data in an SQL database?

As this data looks quite complex, you'd need at least 3 tables in your SQL database:

A users table for the basic info

An address table to store home address details

A hobbies table to store the list of hobbies for each user.

So if you request the user details for a specific ID, the web service would:

  • Get the basic user details by ID (first name, last name)
  • Get the home address details from the address table
  • Get the hobbies list from the hobbies table
  • Assemble everything into JSON, like above, and return it to the user.

If this looks unnecessarily complex and slow to you, you'd be right!

What if there was a database that can store data in a JSON format for me, so I don't have to do the whole conversion thing so my app is easier to code and also faster?

Enter MongoDB!

MongoDB is a database that, instead of relying on SQL technology to store data, the data is stored as documents.

So instead of storing each entry as a "row", like it happens in SQL, it is stored as a "document".

So suppose we need to store the data for Emily, so if we search for the user by ID, it will give us the results in a JSON format, like above.

In MongoDB, all we need to do is to store the data for Emily in a document, in the JSON format we want it to be returned!

{
    "id": 105,
    "firstName": "Emily",
    "lastName": "Matilda",
    "address": {
        "street": "101 High St",
        "city": "Thurgarton",
        "postCode": "NR11 6YD"
    },
    "hobbies": ["Running", "Painting"]
}

So in your favourite programming language, you can just request the document and MongoDB will just give it back to you.

That's it! No conversions, no slow processing of data!

So not only is your data stored in a format that just makes sense, and your app is faster because it doesn't need to process the data, but because of this speed you can just store a HUMONGOUS amound of data.

That is where the name Mongo comes from. It's short for Humongous! 😀

Can't wait to get started? Visit the MongoDB documentation to get started

Explained (34 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
2021 Savvas Stephanides
Buy me a coffee
Some icons from Freepik