Skip to main content
HomeTutorialsSQL

A Comprehensive NoSQL Tutorial Using MongoDB

Learn about NoSQL databases - why NoSQL, how they differ from relational databases, the different types, and design your own NoSQL database using MongoDB.
May 2024  · 9 min read

As the amount of data produced in the digital era explodes, databases are crucial to storing, managing, and analyzing large amounts of data. Broadly, there are two types of databases: SQL and NoSQL databases.

SQL databases use a structured format, and the data is organized into tables with schemas. Unlike SQL databases, NoSQL databases offer flexible schema to store semi-structured and unstructured data. They provide the necessary infrastructure to manage and leverage the vast data generated in today’s digital landscape.

This comprehensive guide will cover the fundamentals of NoSQL databases, and different types of NoSQL databases with examples available in the industry. Once we grasp these basics, we’ll dive into MongoDB, a popular NoSQL database, and learn how to perform CRUD (Create, Retrieve, Update, and Delete) operations.

What is NoSQL?

NoSQL stands for “Not Only SQL” and was created to address the limitations in relational databases. Relational databases follow a rigid structure, have a tabular format, and use SQL (Structured Query Language) for data manipulation.

NoSQL databases offer a more flexible approach for storing semi-structured and unstructured data.

The key advantage of NoSQL databases is that they are suitable for handling large volumes of data with varying structures. NoSQL databases are schema-less, allowing for greater agility and scalability.

However, these benefits come with tradeoffs. They have weak consistency and limited support for complex queries. Despite these limitations, they’re widely used for the benefits they offer and complement the SQL databases.

You can read more about the key differences between SQL and NoSQL databases and some tips on when to choose which from separate tutorial.

Types of NoSQL Databases

To cater to the needs of varying use cases, NoSQL databases use a variety of data models for organizing and accessing the data. Here are 4 common types of NoSQL databases.

  1. Key-value pair-based is the simplest form of NoSQL databases. The data is stored as key and value pairs and can be retrieved using the key. Some examples are Redis and Amazon DynamoDB.
  2. Column-oriented databases organize data in columns rather than in rows. This structure is efficient in retrieving data by columns. Apache Cassandra, HBase, and Google Bigtable are column-oriented.
  3. Graphs-based databases store data in graph structures consisting of nodes, edges, and properties. Neo4j and Amazon Neptune are popular graph-based databases.
  4. Document-oriented store data as JSON-like documents. The structure of a document can vary and even contain nested data. This is a widely used NoSQL database. Examples include MongoDB and CouchDB.

To learn more about the different types of databases, we encourage you to refer to our tutorial, NoSQL Databases: What Every Data Scientist Needs to Know.

After learning about what NoSQL is and the types of NoSQL databases, it’s time for hands-on practice with NoSQL databases.

NoSQL Tutorial: Designing a NoSQL Database for a Blogging Platform

Diving into a practical, real-world example is crucial for grasping and experimenting with the concepts we’ve covered. Let’s walk through a NoSQL tutorial.

Suppose you’re interested in building your blog platform. You’d like to create blog posts, allow comments, and keep them updated. Since you know the data is unstructured, you have an understanding that a NoSQL database such as MongoDB would suit the use case at hand.

MongoDB is an open-source NoSQL database. Instead of using tables, it stores data in JSON-like documents. This allows easy representation of complex structures and relationships. It’s easy to manage different data types including text, numbers, or images, which are likely to exist in blogs.

Let’s start with setting up MongoDB on your machine.

1. Setting up MongoDB

Installing MongoDB is pretty straightforward. We can download and install it from the official MongoDB website. The instructions vary for different operating systems, so check the installation instructions for your operating system from their official documentation.

After installing, start the MongoDB. Simply open the terminal and type the following based on your operating system:

// Linux
sudo systemctl start mongod

//Mac OS
brew services start mongodb-community@7.0

We’ll get an output saying “Successfully started” as shown below.

Output: Starting MongoDB

Output: Starting MongoDB. (This and all images below by Author)

The MongoDB installation includes a MongoDB shell called mongosh which we can use to interact with the MongoDB database. Therefore, we can use it without any additional installation.

Start the MongoDB shell by running the following command in your terminal:

mongosh

Executing the mongosh command will give the below output indicating the connection to your database.

Start the MongoDB shell

Start the MongoDB shell.

With MongoDB installed, we will move on to defining our data model.

2. Defining the data model

We will create a database called “blog” and two collections named “posts” and “comments”. The “posts” collection is to capture the blog entries and the “comments” collection is to store the comments on those posts.

Each post document will have fields for the title, content, author, and creation date, while each comment document will include fields for the comment content, author, and associated post ID.

To list all the existing databases, execute show dbs on the shell. If this is your first time using MongoDB, you will see the below output.

List the existing databases.

By default, we will be using the test database. Although we are in the test database, the database does not get created until we insert a document into a collection in the database.

To create a new database, execute use <database_name>.

In our example, we will use “blog” as the name of our database. Go ahead and execute use blog to create the new database. As mentioned above, executing show dbs command will not show the new database until we insert a collection into the database. Therefore, execute the commands below to create a collection.

// create database
use blog

// create collection
db.createCollection("posts")
db.createCollection("comments")

Executing use blog will switch the database from “test” to “blog.”

Output: Creating database and collection.

After creating collections, we can verify it with show dbs command.

List of databases.

We have successfully created a database and collections.

3. Implementing CRUD operations

MongoDB supports four primary CRUD (Create, Read, Update, Delete) operations for interacting with data:

  1. Create: To create a document in MongoDB, we insert it into a collection using the insertOne() or insertMany() method. Documents can contain any valid JSON data, allowing for flexible schema design.
  2. Read: To read data from MongoDB, we query with find() method. The queries filter documents based on the specified criteria and return the results.
  3. Update: To update the documents, either updateOne() or updateMany() can be used. This method facilitates flexible data manipulation.
  4. Delete: To delete documents from a collection, we use the deleteOne() or deleteMany() method. Deletions are performed based on specified criteria, such as matching a particular field value.

As we have seen the commands, let’s put them into practice through our blogging use case.

4. Creating a blog post

To insert documents into a MongoDB database, there are two methods.

  1. insertOne() : This method inserts one document at a time
  2. insertMany() : Using this method, we can insert many documents at once.

Let’s use both methods to add blog posts to the database. In the code below, we create documents in the “posts” collections. Each document has a title, content, author, and creation date.

// use insertOne() method to insert one document to the "posts" collection
db.posts.insertOne({
   title: "Introduction to NoSQL with MongoDB",
   content: "MongoDB is a NoSQL database that stores data in JSON format",
   author: "Arunn Thevapalan",
   createdAt: new Date()
})

// array of blog post documents
var postsData = [
   {
       title: "Getting Started with MongoDB",
       content: "Learn the basics of MongoDB",
       author: "Arunn Thevapalan",
       createdAt: new Date()
   },
   {
       title: "Advanced MongoDB Techniques",
       content: "Explore advanced features of MongoDB",
       author: "Jim Murphy",
       createdAt: new Date()
   }
];

// use insertMany() to insert multiple documents
db.posts.insertMany(postsData);

This output indicates that the MongoDB server has successfully received and processed the insertOne request.

Output: Insert one document into a collection.

Similarly, the below output indicates the successful processing of insertMany request.

Output: Insert multiple documents into a collection.

With the knowledge of the Create operation, next up is the Read operation.

5. Querying blog posts

We can use the find() method to retrieve data from collections. To retrieve the above blog post from our collection of “posts”, execute the command below.

db.posts.find()

The find method will retrieve all the documents in the “posts” collection. The below output shows all the documents we inserted in the previous step.

Output: Retrieve documents from a collection.

6. Adding a comment to a post

Suppose we want to add a few comments to a post, we can insert a document into the “comments” collection. Comments are associated with the blogs, therefore we will keep a reference to the post ID.

// use an object id from any of the above entries
var postId = ObjectId("664bfc27f74ef93812b2179e")

// insert one comment
db.comments.insertOne({
   postId: postId,
   content: "Informative article!",
   author: "Anne Smith",
   createdAt: new Date()
})

This output indicates that the MongoDB server has received and processed the request.

Output: Create a document referencing another document.

7. Querying comments for a post

To retrieve comments associated with a specific blog post, find() method can be used with a query filter:

// retrieve the above comment

db.comments.find({postId: postId})

The below output shows the comment we inserted in the previous step.

Output: Find a document using the find query.

Once users have created blog posts, they would want to be able to update them at a later time. So next, let’s see how we can update existing documents.

8. Updating a blog post

Similar to inserting, updating can also be done individually or collectively.

  1. updateOne() : This method updates one document at a time
  2. updateMany() : This method updates many documents at once.

Try changing the title of the blog post “Introduction to NoSQL with MongoDB” to “Absolute Beginner Guide to NoSQL with MongoDB”.

Since we know the post ID from one of the earlier steps, execute the below code to change the title. Ensure to update the postId with your document ID.

var postId = ObjectId("664bfc27f74ef93812b2179e");

db.posts.updateOne(
   { _id: postId },
   { $set: { title: "Absolute Beginner guide to NoSQL with MongoDB"} }
);

// verify update
db.posts.find({_id: postId})

The output indicates the request is processed successfully. It found 1 matching document and it modified 1 document.

Output: Updating a document.

9. Deleting a blog post

To delete a blog post deleteOne() can be used. Similar to earlier operations, optionally we can use deleteMany() to delete multiple documents at once.

var postId = ObjectId("664bfc27f74ef93812b2179e");

// delete post
db.posts.deleteOne({ _id: postId });

// verify deletion -> no output
db.posts.find({_id: postId})

The output confirms the successful deletion.

Output: Delete a document from a collection

Output: Delete a document from a collection

Of course, we have simplified the design of a blogging platform, which is generally complex with various other operations such as highlights, likes, claps, recommendations, etc. But all of these operations are based on one of the CRUD operations and we have a good grasp of these underlying concepts now. It’s your turn now to explore and experiment with more features based on these operations.

Conclusion

This tutorial introduced you to NoSQL databases. We started with the basic concepts, the definition of NoSQL databases, how they differ from relational databases, and their types.

We further expanded it to cover MongoDB, a widely used document-based database. Finally, we got our hands dirty with a simple example to design a database for our blog platform.

It’s normal to get stuck when designing a database for a new use case, and we encourage you to refer to the official documentation to know the updated functions and examples.

Here are some more in-depth courses and tutorials if you are eager to broaden your knowledge of NoSQL.


Photo of Arunn Thevapalan
Author
Arunn Thevapalan
LinkedIn
Twitter

As a senior data scientist, I design, develop, and deploy large-scale machine-learning solutions to help businesses make better data-driven decisions. As a data science writer, I share learnings, career advice, and in-depth hands-on tutorials.

Topics

Continue Learning with DataCamp

Course

Introduction to NoSQL

4 hr
2.3K
Conquer NoSQL and supercharge data workflows. Learn Snowflake to work with big data, Postgres JSON for handling document data, and Redis for key-value data.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

SQL vs NoSQL Databases: Key Differences and Practical Insights

Discover how to decide between SQL and NoSQL databases in data science and application development. Learn their strengths, use cases, and industry applications.

Kevin Babitz

15 min

blog

NoSQL Databases: What Every Data Scientist Needs to Know

Find out what NoSQL databases are used for, why data scientists use them, and a list of the best NoSQL databases available.
Zoumana Keita 's photo

Zoumana Keita

12 min

blog

Types of Databases: Relational, NoSQL, Cloud, Vector

The main types of databases include relational databases for structured data, NoSQL databases for flexibility, cloud databases for remote access, and vector databases for machine learning applications.
Moez Ali's photo

Moez Ali

15 min

tutorial

Introduction to DynamoDB: Mastering NoSQL Database with Node.js | A Beginner's Tutorial

Learn to master DynamoDB with Node.js in this beginner's guide. Explore table creation, CRUD operations, and scalability in AWS's NoSQL database.
Gary Alway's photo

Gary Alway

11 min

tutorial

MySQL Tutorial: A Comprehensive Guide for Beginners

Discover what MySQL is and how to get started in one of the most popular database management systems.
Javier Canales Luna's photo

Javier Canales Luna

15 min

tutorial

Introduction to MongoDB and Python

In this tutorial, you'll learn how to integrate MongoDB with your Python applications.
Derrick Mwiti's photo

Derrick Mwiti

12 min

See MoreSee More