Course

Terminology

What's a database made of?

First let's get some naming conventions out of the way. Databases in Mongo are made of .

Collections are made of . And Documents are essentially JSON objects.

What does this all mean? Let's break it down.

Collections

A collection is a group of documents stored in MongoDB. It is the equivalent of a table in a relational database. Typically, each collection in a database will store documents of a similar type or structure. You might have a collection, a collection, a collection, etc...

Documents

A document is a JSON object that contains data. It is the equivalent of a row in a relational database. Documents are stored in collections and can have different structures. Each document in a collection is unique and has a unique identifier called .

In our previous example where we may have a collection, each document in that collection would represent a user. The document would contain fields such as name, email, age, etc...

Example

Below is an example of how your data might be stored in a MongoDB collection. Below is a collection called that contains a couple documents representing users:

//
  {
    "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
    "name": "John Doe",
    "email": "john@example.com",
    "age": 30,
    "address": {
      "street": "123 Main St",
      "city": "New York",
      "country": "USA"
    },
    "hobbies": ["reading", "traveling"],
  },
  {
    "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
    "name": "Jane Doe",
    "email": "jane@example.com",
    "age": 25,
    "address": {
      "street": "456 Elm St",
      "city": "Los Angeles",
      "country": "USA"
    },
    "hobbies": ["swimming", "cooking"],
  }

In this example, the "users" collection contains a two documents representing two different users. Let's break down the structure:

: This is the unique identifier for the document, automatically generated by MongoDB. It is of type ObjectId.

: The name of the user, stored as a string.

: The email address of the user, stored as a string.

: The age of the user, stored as a number.

: An embedded document ( another JSON object ) that contains the user's address information.

: The street address, stored as a string.

: The city, stored as a string.

: The country, stored as a string.

: An array of strings representing the user's hobbies.

Document Structure

In the example above- we had two users with identical structures. Meaning both users had the same fields like name or email. But in Mongo, documents in a collection can have different structures.

For example, the following documents are valid in the same collection:

//
    {
      "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
      "name": "John Doe",
      "email": "email@email.com",
      "age": 30,
      "hobbies": ["reading", "traveling"],
    },
    {
      "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
      "name": "Jane Doe",
      "email": "email2@email.com",
      "phone": "123-456-7890",
    }

In this example, the first document has fields for name, email, age, and hobbies, while the second document has fields for name, email, and phone. The documents do not have to have the same fields or structure in order to be stored in the same collection.

This flexibility is one of the key features of MongoDB, and allows for easy and fast development and iteration on your data model.

Warning

Flexibility is a double-edged sword! It can make development easier, but it can also lead to inconsistencies in your data if you're not careful. It's important to have a clear understanding of your data model and how your documents are structured in order to avoid issues down the line.

Can you think of any potential issues that might arise from having documents with different structures in the same collection?

Multiple Collections

A database can have multiple collections, each containing documents. This allows you to organize your data in a way that makes sense for your application.

For example, you might have a database for an e-commerce application with collections for users, products, orders, and reviews. Each collection would contain documents representing the data for that particular entity.

//
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "name": "John Doe",
},
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
  "name": "Jane Doe",
}
//
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "name": "iPhone 12",
  "price": 999,
},
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
  "name": "MacBook Pro",
  "price": 1999,
}
//
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "user_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "products": [
    {
      "product_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
      "quantity": 2,
    },
    {
      "product_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
      "quantity": 1,
    }
  ],
  "total": 2997,
}
//
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "product_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "user_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4a"),
  "rating": 5,
  "comment": "Great product!",
},
{
  "_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
  "product_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
  "user_id": ObjectId("60a1b0a1f4b4d42a0c7d1f4b"),
  "rating": 4,
  "comment": "Good product!",
}

Review: Database vs Collection vs Document

  • Database: A database is a container for collections. It is the highest level of organization in MongoDB. A MongoDB server can have multiple databases, each containing multiple collections.

  • Collection: A collection is a group of documents. It is the equivalent of a table in a relational database. Collections do not enforce a schema, meaning that documents in a collection can have different structures.

  • Document: A document is a JSON object that contains data. It is the equivalent of a row in a relational database. Documents are stored in collections and can have different structures.

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

What is an ODM?