Skip to Content
Lesson 1

Introduction to NoSQL & MongoDB

Workplace Context

Your team is building a new application that requires a flexible data model to handle large volumes of unstructured data. The development team has chosen MongoDB, a popular NoSQL database. As a developer, you need to understand the core concepts of NoSQL and MongoDB to contribute effectively to the project.


Learning Objectives

By the end of this lesson, you will be able to:

  • Explain the differences between SQL and NoSQL databases.
  • Describe the structure of MongoDB, including documents and collections.
  • Interact with a MongoDB database using MongoDB Compass.

What is NoSQL?

NoSQL, which stands for “Not Only SQL,” refers to a type of database that is not built on the traditional relational (SQL) model. While SQL databases use tables, rows, and columns to structure data with a predefined schema, NoSQL databases use more flexible data models. This makes them ideal for applications that require high performance, scalability, and the ability to handle unstructured or semi-structured data.

Key Differences Between SQL and NoSQL

FeatureSQL (e.g., PostgreSQL, MySQL)NoSQL (e.g., MongoDB, Cassandra)
Data ModelRelational (tables, rows, columns)Document, key-value, column-family, or graph
SchemaFixed, predefined schemaDynamic or flexible schema
ScalabilityVertically scalable (increase server hardware)Horizontally scalable (distribute load across servers)
Use CasesTransactional systems, data warehousingBig data, real-time applications, content management


Introduction to MongoDB

MongoDB is one of the most popular NoSQL databases. It is a document-oriented database, which means it stores data in flexible, JSON-like documents. This model maps directly to objects in your application code, making it easy to work with.

Core Concepts

  • Document: A document is the basic unit of data in MongoDB, similar to a row in a SQL database. It is a set of key-value pairs, where keys are strings and values can be various data types, including other documents and arrays.

    { "_id": "60c72b2f9b1e8a5f4d6e8b21", "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925, "genres": ["Novel", "Fiction"] }
  • Collection: A collection is a group of related documents, similar to a table in a SQL database. However, unlike a table, a collection does not enforce a strict schema. This means the documents within a single collection can have different fields.

  • Database: A database is a container for collections. Each database gets its own set of files on the file system. A single MongoDB server can host multiple databases.


Activities

Activity 1: Explore a Database with MongoDB Compass

MongoDB Compass is a graphical user interface (GUI) that allows you to interact with your MongoDB data without writing scripts. In this activity, you will connect to a sample database and explore its collections and documents.

  1. Download and Install MongoDB Compass: If you haven’t already, download it from the MongoDB website.
  2. Connect to a Sample Cluster: Open Compass and use the following connection string to connect to a read-only sample dataset:

mongodb+srv://ps-learner:read-only-access@cluster0.4fx7ve3.mongodb.net/

  1. Explore Collections: Once connected, you will see a list of databases. Navigate into the sample_mflix database.

  2. Inspect Documents: Click on the movies collection. Browse through the documents and observe their structure. Notice how some documents have fields that others do not.

  3. Filter Data: Use the filter bar at the top to query the data. For example, try to find all movies released in the year 1999 by using the following filter:

    { "year": 1999 }

Knowledge Check

What is the main advantage of a NoSQL database like MongoDB over a SQL database?

  • Select an answer to view feedback.

In MongoDB, what is a 'collection' analogous to in a SQL database?

  • Select an answer to view feedback.

What is a MongoDB document?

  • Select an answer to view feedback.

Summary

In this lesson, you learned about the fundamental concepts of NoSQL databases and how they differ from traditional SQL databases. You were introduced to MongoDB’s document-oriented model, including its core components: documents, collections, and databases. Finally, you gained hands-on experience by exploring a sample database with MongoDB Compass. This foundation will be crucial as you move on to building applications with MongoDB.


References


Additional Resources