In this tutorial, you’ll learn how to integrate MongoDB with your Python applications.
MongoDB is a leading open-source N0SQL database that is written in C++. This tutorial will give the reader a better understanding of MongoDB concepts needed in integrating MongoDB in your Python applications.
The SQL vs. NoSQL Difference
SQL databases use Structured Query Language(SQL) in defining and manipulating data. When using SQL, we need a Relational Database Management System(RDBMS) server such as SQL Server, MySQL server or MS Access. Data in RDBMS is stored in database objects called tables. A table is a collection of related data entries, and it consists of columns and rows.

A NoSQL database has a dynamic schema for unstructured data. In NoSQL, data is stored in several ways: it can be column-oriented, document-oriented, graph-based or organized as a key-value store. A NoSQL database has the following advantages:
- Documents can be created without having to first define their structure
- Each document can have its own unique structure
- The syntax can vary from database to database
- Large volumes of structured, semi-structured, and unstructured data
- Object-oriented programming that is easy to use and flexible
- It is horizontally scalable
NoSQL Database Types
The following are the different types of NoSQL databases:
- Document databases pair each key with a complex data structure known as a document. A document is a set of key-value pairs. MongoDB is an example of a document store database. A group of MongoDB documents is known as a collection. This is the equivalent of an RDBMS table.
- Graph stores are used to store information about networks of data, for instance, social connections. Graph stores include Neo4J and Giraph.
- Key-value stores databases store every single item in the database as a key together with its value. Examples of key-value stores are Riak and Berkeley DB. Some key-value stores, such as Redis, allow each value to have a type, such as an integer, which adds functionality.
- Wide-column stores such as Cassandra and HBase are optimized for queries over large datasets, and store columns of data together, instead of rows.
MongoDB and Python
In order to start using MongoDB, we first have to install it. Installation instructions are found at the official MongoDB documentation. To run a quick install on Ubuntu run the commands below: Learn more from MongoDB Training
sudo apt update sudo apt install -y mongodb
Once this is done we’ll check the service and database by running this command on the terminal:
sudo systemctl status mongodb
mongodb.service - An object/document-oriented database Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset:
Now that we have MongoDB installed we need a way to interact with it in our Python code. The official Python MongoDB driver is called PyMongo. We can install it using pip as shown below:
pip install pymongo
Its possible for us to interact with MongoDB from the terminal, however for the purposes of this tutorial we’ll run all our code in a Jupyter Notebook.
Making a Connection with MongoClient
The first thing we need to do is import pymongo. The import should run without any errors to signify that we’ve done our installation well.
import pymongo
Establishing a connection in MongoDB requires us to create a MongoClient to the running MongoDB instance.
frompymongoimportMongoClient
client = MongoClient()
The above code will connect to the default host and port, but we can specify the host and port as shown below:
client = MongoClient("localhost",27017)
MongoDB also has a URI format for doing this.
client = MongoClient('mongodb://localhost:27017/')
Creating a Database
To create a database in MongoDB, we use the MongoClient instance and specify a database name. MongoDB will create a database if it doesn’t exist and connect to it.
db = client['datacampdb']
It is important to note that databases and collections are created lazily in MongoDB. This means that the collections and databases are created when the first document is inserted into them. Learn more skills from MongoDB Certification
Data in MongoDB
Data in MongoDB is represented and stored using JSON-Style documents. In PyMongo we use dictionaries to represent documents. Let’s show an example of a PyMongo document below:
article = {"author": "Derrick Mwiti",
"about": "Introduction to MongoDB and Python",
"tags":
["mongodb", "python", "pymongo"]}
Inserting a Document
To insert a document into a collection, we use the insert_one() method. As we saw earlier, a collection is similar to a table in RDBMS while a document is similar to a row.
articles = db.articles
result = articles.insert_one(article
When the document is inserted, a special key _id is generated and its unique to this document. We can print the document ID as shown below:
print("First article key is: {}".format(result.inserted_id))
First article key is: 5ba5c05e2e8ca029163417f8
The articles collection is created after inserting the first document. We can confirm this using the list_collection_names method.
db.list_collection_names()
['articles', 'user']
We can insert multiple documents to a collection using the insert_many() method as shown below.
article1 = {"author": "Emmanuel Kens",
"about": "Knn and Python",
"tags":
["Knn","pymongo"]}
article2 = {"author": "Daniel Kimeli",
"about": "Web Development and Python",
"tags":
["web", "design", "HTML"]}
new_articles = articles.insert_many([article1, article2])
print("The new article IDs are {}".format(new_articles.inserted_ids))
The new article IDs are [ObjectId('5ba5c0c52e8ca029163417fa'), ObjectId('5ba5c0c52e8ca029163417fb')]
Retrieving a Single Document with find_one()
find_one() returns a single document matching the query or none if it doesn’t exist. This method returns the first match that it comes across. When we call the method below, we get the first article we inserted into our collection.
print(articles.find_one())
{'_id': ObjectId('5ba5c0b52e8ca029163417f9'), 'author': 'Derrick Mwiti', 'about': 'Introduction to Mo
To get in-depth knowledge, enroll for a live free demo on MongoDB Online Training