Tutorial

How To Perform CRUD Operations in MongoDB Using PyMongo on Ubuntu 20.04

MongoDBPythonUbuntu 20.04
Not using Ubuntu 20.04?
Choose a different version or distribution.

The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.

Introduction

MongoDB is a general-purpose, document-oriented, NoSQL database program that uses JSON-like documents to store data. Unlike tabular relations used in relational databases, JSON-like documents allow for flexible and dynamic schemas while maintaining simplicity. In general, NoSQL databases have the ability to scale horizontally, making them suitable for big data and real-time applications.

A database driver or connector is a program that connects an application to a database program. To perform CRUD operations in MongoDB using Python, a driver is required to establish the communication channel. PyMongo is the recommended driver for working with MongoDB from Python.

In this guide, you will write a Python script that creates, retrieves, updates, and deletes data in a locally installed MongoDB server on Ubuntu 20.04. In the end, you will acquire relevant skills to understand the underlying concepts in how data moves across MongoDB and a Python application.

Prerequisites

Before you move forward with this guide, you will need the following:

Step 1 — Setting Up PyMongo

In this step, you will install PyMongo, the recommended driver for MongoDB from Python. As a collection of tools for working with MongoDB, PyMongo facilitates database requests using syntax and an interface native to Python.

To enable PyMongo, open your Ubuntu terminal and install from the Python Package Index. It is recommended to install PyMongo within a virtual environment in order to isolate your Python project. Refer to this guide if you missed how to set up a virtual environment in the prerequisites.

  • pip3 install pymongo

pip3 refers to the Python3 version of the popular pip package installer for Python. Note that within the Python 3 virtual environment you can use the command pip instead of pip3.

Now, open the Python interpreter with the command below. The interpreter is a virtual machine that operates like a Unix shell, where you can execute Python code interactively.

  • python3

You are in the interpreter when you get an output similar to what’s below:

Output
Python 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

With a successful output, import pymongo in the Python interpreter:

  • import pymongo

Using the import statement, you can access the pymongo module and its code in your terminal. The import statement will run without raising exceptions.

On the next line, import getpass.

  • from getpass import getpass

getpass is a module for managing password inputs. The module prompts you for a password without showing an input, and adds a security mechanism to prevent displaying passwords as plaintext.

Here, make a connection with MongoClient to enable a MongoDB instance of your database. Declare a variable client to hold the MongoClient instance with host, username, password, and authMechanism as arguments:

  • client = pymongo.MongoClient('localhost', username='username', password=getpass('Password: '), authMechanism='SCRAM-SHA-256')

To connect to MongoDB with authorization enabled, MongoClient requires four arguments:

  • host - the hostname of the server on which MongoDB is installed. Since Mongo is local in this context, use localhost.
  • username and password - authorization credentials created after enabling authentication in MongoDB.
  • authMechanism - SCRAM-SHA-256 is the default authentication mechanism supported by a cluster configured for authentication with MongoDB 4.0 or later.

Once you’ve established the client connection, you can now interact with your MongoDB instance.

Step 2 — Testing Databases and Collections

In this step, you will get familiar with NoSQL concepts such as collections and documents as applied to MongoDB.

MongoDB supports managing multiple independent databases within a MongoClient instance. You can access or create a database using attribute style on a MongoClient instance. Declare a variable db and assign the new database as an attribute of client:

  • db = client.workplace

In this context, the workplace database keeps track of employee records you will add such as the employee’s name and role.

Next, create a collection. Like tables in relational databases, collections store a group of documents in MongoDB. In your Python interpreter, create an employees collection as an attribute of db and assign it to a variable of the same name:

  • employees = db.employees

Create the employees collection as an attribute of db and assign it to a variable of the same name.

Note: In MongoDB, databases, and collections are created lazily. This means that none of the above codes are actually executed until the first document is created.

Now that you’ve reviewed collections, let’s look at how MongoDB represents documents, the basic structure for representing data.

Step 3 — Performing CRUD Operations

In this step, you will perform CRUD operations to manipulate data in MongoDB. Create, retrieve, update, and delete (CRUD) are the four basic operations in computer programming that one can perform to create persistent storage.

To represent data in Python as JSON-like documents, dictionaries are used. Create a sample employee record with name and role attributes:

  • employee = {
  • "name": "Sammy",
  • "role": "Developer"
  • }

As you can see, Python dictionaries are very similar in syntax to JSON documents. PyMongo converts Python dictionaries to JSON documents for scalable data storage.

At this point, insert the employee record into the employees collection:

  • employees.insert_one(employee)

Calling the insert_one() method on the employees collection, provide the employee record created earlier to be inserted. A successful insertion should return a successful output like below:

Output
<pymongo.results.InsertOneResult object at 0x7f8c5e3ed1c0>

Now, verify you’ve successfully inserted the employee record and the collection. Make a query to find the employee you just created:

  • employees.find_one({"name": "Sammy"})

Calling thefind_one() method on the employees collection with a name query returns a single matching document. This method is useful when you have only one document, or when you are interested in the first match.

The output should look similar to this:

Output
{'_id': ObjectId('606ae5b2358ddf640da46894'), 'name': 'Sammy', 'role': 'Developer'}

Note: When a document is inserted, a unique key _id is automatically added to the document if it does not already contain an _id key.

If the need arises to modify existing documents, use the update_one() method. The update_one() method requires two arguments, query and update:

  • query - {"name": "Sammy"} - PyMongo will use this query parameter to find documents with elements that match.
  • update - { "$set": {"role": "Technical Writer"} } - The update parameter implements the $set operator, which replaces the value of a field with the specified value.

Call the update_one() method on the employees collection:

  • employees.update_one({"name": "Sammy"}, { "$set": {"role": "Technical Writer"} })

A successful update will return an output similar to this:

Output
<pymongo.results.UpdateResult object at 0x7f8c5e3eb940>

To delete a single document, employ the delete_one() method. delete_one() requires a query parameter which specifies the document to delete. Execute the delete_one() method as an attribute of the employees collection with the name Sammy as a query parameter.

  • employees.delete_one({"name": "Sammy"})

This will delete the only entry you have in your employees collection.

Output
<pymongo.results.DeleteResult object at 0x7f8c5e3c8280>

Using the find_one() method again, it is apparent that you’ve successfully deleted Sammy’s employee record as nothing prints to the console.

  • employees.find_one({"name": "Sammy"})

insert_one(), find_one(), update_one(), and delete_one() are great ways of getting started with performing CRUD operations in MongoDB with PyMongo.

Conclusion

In this guide, you have explored how to set up and configure PyMongo, the database driver, to connect Python code to MongoDB, as well as creating, retrieving, updating, and deleting documents. Although this guide focuses on introductory concepts, PyMongo offers more powerful and flexible ways of working with MongoDB. For instance, you can make bulk inserts, query for more than one document, add indexes to queries, and many more.

To learn more about MongoDB management, see How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 20.04 and How To Import and Export a MongoDB Database on Ubuntu 20.04.

Creative Commons License