Python

How to Create a Database in MongoDB Using Python

There’s no doubt that Python is a powerful—and popular—programming language capable of handling any project we throw its way. It is very flexible and can adjust to suit various development environments like penetration testing to web development and machine learning.

When coupled to large applications such as those that require databases, Python adds more functionality and can be hard to work with, especially for beginners.

Python knows this add provides us with better ways to add databases to our projects without compromising our workflow using a simple and intuitive NoSQL database. Using Python and a popular NoSQL database, MongoDB, development becomes more comfortable and, all in all, fun.

This article will go over various MongoDB database concepts to give you a firm understanding of what it entails. After that, we will cover how to install MongoDB on Linux and show you how to use Python to interact with MongoDB.

Let us get started:

A Basic Introduction to MongoDB

MongoDB is an open-source, document-based database that provides high scalability and flexibility. Like most NoSQL databases, MongoDB uses JSON to store the data, making it one of the most flexible and easy databases to work with because it requires no schema.

Thanks to its flexibility and learning ease, developers often use MongoDB for large projects requiring fast data read and write speeds. It comes prepackaged with drivers for popular programming languages, thus eliminating the need to learn new programming languages before using it.

NOTE: If you are not familiar with the concept of SQL and NoSQL databases, check out the resource provided below:

https://www.mongodb.com/nosql-explained/nosql-vs-sql

Learning how to work with MongoDB is an essential skill, mainly because we live in a data-driven world where, as a programmer, you’ll be working with data 90% of the time—if not more.

It’s good to note that there’s more to MongoDB than what we’ll cover in today’s guide. Consider checking the official documentation and external resources to learn more.

How to Install MongoDB on Linux (Debian 10)

Let’s quickly go over how to install the MongoDB Community Edition on Debian 10.

NOTE: Ensure you uninstall the MongoDB package maintained by Debian because it’s not the official MongoDB package, and failure to uninstall it might conflict with the latest version:

First, ensure that your system is up to date, which you can do using the command:

sudo apt-get update && sudo apt-get upgrade -y

Next, install GnuPG and import the MongoDB repository public key using the command:

sudo apt-get install gnupg && wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

Add a file list in the sources.list.d directory using the command:

echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Update your local repositories and install the mongodb-org package.

sudo apt-get update && sudo apt-get install mongodb-org

Once you have successfully installed MongoDB, start the service using the system as follows:

sudo systemctl start mongod

You can also start a mongo shell using the command mongo

How to Use Python to Work with MongoDB

Let’s now discuss how to use Python to work with MongoDB.

At this point, I’ll assume that you have Python already setup and installed on your system.

Since this is a quick starter guide, not a comprehensive MongoDB guide, we will only discuss the basics of using PyMongo to interact with the database.

PyMongo

We shall look at the standard method when interacting with MongoDB to use the official Python driver, PyMongo. PyMongo is a very efficient way to work with Python and MongoDB and is one of the best ways to get started.

NOTE: Although this getting started guide has tons of details, you should consider looking at the official documentation to learn more.

Here’re some resources for your consideration:

https://pymongo.readthedocs.io/en/stable/index.html
https://pypi.org/project/pymongo/
https://docs.mongodb.com/drivers/pymongo

How to install PyMongo

As usual, the first thing we need to do is install PyMongo Driver in our Python environment; you can use conda or pip.

To install, use the command:

pip install pymongo

Wait until the required transactions complete and you have PyMongo successfully installed on your system. To confirm, fire up an interactive python shell and execute the command:

>>> import pymongo

Once it runs successfully with no errors, you have successfully installed PyMongo, and we can move to the next section.

How to use PyMongo to connect to MongoDB

To connect to MongoDB using PyMongo, we use the MongoClient object and create an instance to mongod, the main daemon process for the MongoDB.

>>> from pymongo import MongoClient
>>> client = MongoClient(“localhost”, 27017)

The above code snippet imports the MongoClient object from PyMongo and then creates a client instance to mongod. If you do not need to specify the target host and port, you can leave it empty, thus establishing a default connection.

You can also use MongoDB URI format as:

>>> client = MongoClient("mongodb://localhost:27017/")

Both these methods will do the same thing; it only depends on which you prefer to use in your project.

How to create a database using PyMongo

Using PyMong to create a database in MongoDB is relatively straightforward. All you have to do is query the database, and if it does not exist, MongoDB will create it automatically.

Consider the code below:

>>> from pymongo import MongoClient
>>> client = MongoClient("localhost", 27017)
>>> database = client["test_database"]

You can also use the attribute method, instead of the dictionary method, to access a database.

>>> database = client.test_database

Unlike other Databases, in MongoDB, a database is not fully created until collections (data) have been saved—think of collections as tables in SQL databases.

How to Insert documents into a database

As mentioned at the beginning of this tutorial, MongoDB stores data as JSON documents stored in a collection—think of documents as rows in SQL databases.

In PyMongo, we use python dictionaries to represent a document. Consider the following example code:

from pymongo import MongoClient

client = MongoClient("localhost", 27017)

database = client["movie_db"]

movies = database.movies

movie_ = {

    "title": "Mr. Robot",

    "Starring": "Rami Malek, Christian Slater, Carly Chaikin",

    "created": "Sam Esmail",

    "Year": "2016"

}

id = movies.insert_one(movie_).inserted_id

print(id)

This code should print the id as shown:

5ff57066fee7e4e965f02267

When we create a new document and add it to the collection, a special key or _id gets created. The value of the id must be unique in the set collection.

We can verify that the database, collection, and document exist by using a simple query.

>>> database.list_collection_names()

['movies']

Programmers are nothing if not efficient—and lazy.

Because of this default inclination, we can also use the insert_many() method to add multiple documents instead of a single document with the insert_one ()method.

Consider the code below:

from pymongo import MongoClient

client = MongoClient("localhost", 27017)

database = client["movie_db"]

movies = database.movies

movies_ = [

     {

    “title”: “Mr. Robot,

    “Starring”: “Rami Malek, Christian Slater, Carly Chaikin,

    "created": "Sam Esmail",

    “Year”: “2016.”

},

{

    “title”: “The Big Bang Theory,

    “Starring”: “Jim Parsons, Kaley Cuoco, Johnny Galecki, Kunal Nayyar, Simon Helber, Mayim Bialik, Melissa Rauch,

    “Created”: “Chuck Lorre, Bill Prady,

    “Year”: “2007.”

},

{

    "title": "Star Trek: Original Series",

    “Starring”: “William Shatner, Leonard Nimoy, Nichelle Nicholas,

    "Created": "Gene Roddenberry",

    “Year”: “1966.”

}

]

ids = movies.insert_many(movies_).inserted_ids

print(ids)

This should give you the _ids of the inserted document as shown below:

[ObjectId(‘5ff5749fbbc71282a634303d’),ObjectId(‘5ff5749fbbc71282a634303e’), ObjectId(‘5ff5749fbbc71282a634303f’)]

How to fetch documents

Getting documents from a collection is also very simple, and using the find_one() method, we can accomplish this in one line of code. Consider the example below to collect Gene Roddenberry’s movies.

print(movies.find_one({"Created": "Gene Roddenberry"}))

This should retrieve all the movies created by Gene Roddenberry in that database.

{'_id': ObjectId('5ff57472e027b7cd22b1f2f4'), 'title': 'Star Trek: Original Series', 'Starring': 'William Shatner, Leonard Nimoy, Nichelle Nicholas,', 'Created': 'Gene Roddenbery', 'Year': '1966'}

You can look at a GUI display of the database and the collections. Here’s a screenshot captured from MongoDB Compass on Windows.

Conclusion

To use the Python programming language effectively when working with databases, you will need a flexible and efficient database for your project. MongoDB is the right choice. Please do not mistake this to mean MongoDB is the best database choice ever. It has shortcomings but is also an ideal choice for most use cases.

This guide has given you everything you need to start experimenting with databases using your language of choice.

In parting, consider reading documentation and manuals to gain a deeper understanding of this particular topic.

About the author

John Otieno

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list