Get startedGet started for free

Introduction to MongoDB

1. Introduction to MongoDB

Welcome! I’m Filip Schouwenaars, your instructor for this MongoDB in Python course. I’ve authored some of DataCamp’s most popular courses on Python, R, and SQL—and I’m excited to help you discover the power of MongoDB.

2. Data is everywhere!

Data is at the heart of any application, or any attempt to describe the world around us. And how we store that data really affects how easily we can access and analyze it later. Let’s look at a simple example.

3. Example: Student course data

Say you’re keeping track of students and the courses they take. Instead of repeating the course details every time a student signs up, you do something smarter:

4. How to store data

You make a list of students, with their name and a unique ID.

5. How to store data

You make a separate list of courses, with a title, a short description, and a unique course code.

6. How to store data

Then, you create a third list that links students to courses, just using their IDs and course codes. This avoids repeating the same information over and over. If five students take the same course, you don't write the course title five times in full. Instead, you just refer to it by its code. This style of organizing data is known as the "relational model", and it is used by relational databases, like MySQL and PostgreSQL.

7. When relational databases fall short

So relational databases are great for structured, consistent data. But what if your data doesn't fit neatly into rows and columns anymore? Say you want to store extra details about each student's progress; things like quiz scores, project feedback, notes from instructors, maybe even drafts of their work. Not every student has the same data, and some of it - like project feedback - would be deeply structured or change over time. In a relational database, you'd need to create a whole set of new tables, for every thing you're storing. Then you'd connect them with foreign keys, joins, and careful rules to keep everything in sync. It works, but it gets complicated, fast.

8. Enter MongoDB

This is where NoSQL databases like MongoDB come in. MongoDB is a document database, which means it stores data in self-contained records called documents. MongoDB has what we call a flexible schema, which means that you don’t have to lock yourself into a fixed data structure up front. Each student can have their own document with as much or as little detail as needed, without having to rearchitect the whole system if you want to add or update something. In our example, it’s like having one rich, self-contained file per student, with names, courses, feedback, and more—all in one place. And in this course, you’ll learn how to work with MongoDB, right from inside Python.

9. BSON, a special kind of JSON

You’ll often hear that MongoDB stores data in JSON. That’s partly true—but under the hood, it actually uses a format called BSON. BSON stands for Binary JSON. It’s very similar to regular JSON, but it’s faster for computers to read and write, and it supports more data types, like dates, binary data, and even embedded documents. When interacting with MongoDB, you'll still use JSON-style syntax, but just know that MongoDB stores it as BSON, so it can handle more complex data and run more efficiently.

10. Connecting to MongoDB

To work with MongoDB in Python, we’ll use pymongo, the official MongoDB library. To make a connection, we use the MongoClient class. We start by importing MongoClient, and then create a client instance. If you don’t pass any arguments, pymongo will try to connect to a MongoDB server running locally on your machine. That’s perfect for this course and for testing things out. In production, though, you'll often connect to a cloud database, like MongoDB Atlas. Here's what that would look like.

11. Let's practice!

With this theory behind us, it's time for your first interactive exercises. Have fun!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.