The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.
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.
Before you move forward with this guide, you will need the following:
One Ubuntu 20.04 server with at least 1GB of RAM set up by following the Ubuntu 20.04 initial server setup guide, including a sudo non-root user and a firewall.
A Python 3 installation with an environment set up on Ubuntu 20.04 using this quick start guide.
An installation of the latest MongoDB server compatible with Ubuntu 20.04 using the How To install MongoDB on Ubuntu 20.04 guide.
A security configuration of MongoDB on Ubuntu 20.04 to prevent unauthorized access.
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 Python 3 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:
OutputPython 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:
localhost
.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.
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.
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 the find_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
:
{"name": "Sammy"}
- PyMongo will use this query parameter to find documents with elements that match.{ "$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.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.