Chuck's Academy

Database

Data Modeling in NoSQL

In this chapter, we will explore the data modeling process in NoSQL databases, focusing on MongoDB. Unlike relational databases, where the schema is rigidly defined through tables and relationships, NoSQL databases allow for greater flexibility in modeling data, giving us the ability to adapt to unstructured or frequently changing data.

Collections and Documents

In MongoDB, data is stored in collections, which are equivalent to tables in relational databases. Each collection contains multiple documents, which are flexible data structures in JSON format.

Document Example

A document in MongoDB is similar to a JSON object. Here's an example of a document that could be in a collection called users:

json
"This document represents a user named Alice, with an _id field that acts as a unique identifier, and an array called orders that contains two orders placed by the user."

Flexible Structure

One of the greatest advantages of MongoDB is its flexible structure. Unlike SQL, where every row in a table must follow the same schema, in MongoDB each document can have a different set of fields. For example, we can add a new document to the users collection with additional fields or fewer fields than the previous one:

json
"This document represents a user named Bob, with fewer fields than Alice's document, as it does not include age or orders. MongoDB does not require all documents to have the same fields."

Relationships in NoSQL

Although MongoDB does not use relationships in the traditional sense of relational databases, we can establish relationships between documents using references or embeddings.

Document Embedding

We can embed documents within other documents when data is closely related and we want to avoid performing multiple queries. In the previous example, the orders are embedded in the users document, making it easy to query all of a user's orders in a single operation.

json
"Here we are embedding an array of orders within Charlie's document, allowing us to access user data and their orders in a single query."

Document References

In some cases, data may be more separated and of large size, so it is preferable to use references between documents instead of embeddings. For example, we can have a users collection and an orders collection, where orders documents reference users through the user_id field:

json
"This document in the orders collection references the user with id equal to one, using the user_id field."

Schema Design for NoSQL

Schema design in NoSQL databases is heavily dependent on application needs. Some common design patterns include:

  • Embeddings: Used when data is closely related and frequently queried together.
  • References: Used when data is large or needed independently in different parts of the application.
  • Arrays: MongoDB allows storing arrays within documents, which is useful for lists like tags, shopping cart items, or orders.

Blog Application Modeling Example

If we are designing a blog application, we might have two collections: one for authors and another for articles. Articles documents could have a reference to the authors:

json
"This document represents an article with its title, content, author identifier, and a list of tags. The author_id field references the author in the authors collection."

Best Practices in NoSQL Data Modeling

Some best practices to follow when modeling data in MongoDB include:

  • Optimize for queries: Design the schema so that the most common queries are efficient.
  • Avoid extreme normalization: Unlike SQL, in NoSQL it is not necessary to excessively normalize data. Embeddings are preferable when data is closely related.
  • Consider document size: MongoDB has a 16 MB limit per document, so it's important to consider this limit when deciding on embeddings.

Summary

In this chapter, we have learned how to model data in MongoDB using collections and documents. We also explored different modeling techniques, such as embeddings and references, and reviewed best practices to optimize schema design. In the next chapter, we will delve into data creation operations in MongoDB and how to insert documents into our collections.


Ask me anything