The author choosesFree and Open Source FundAs aWrite for DOnationsPart of the program accepts donations.
Introduction to the
MongoDB is a general-purpose, document-oriented, NoSQL database program that uses JSON-like documents to store data. Unlike the table relationships used in relational databases, JSON-like documents allow 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. In order to use Python to perform CRUD operations in MongoDB, you need a driver to set up a communication channel. PyMongo is the recommended driver for working with MongoDB from Python.
In this tutorial, you’ll write a Python script to create, retrieve, update, and delete data in a locally installed MongoDB server on Ubuntu 20.04. Finally, you’ll gain skills to understand the basic concepts of how data moves through MongoDB and Python applications.
The premise condition
Before you can continue with this guide, you will need the following.
-
An Ubuntu 20.04 server, with at least 1GB of ram, is configured according to the Ubuntu 20.04 Initial Server Setup guide, including a sudo non-root user and a firewall.
-
Install Python 3 on Ubuntu 20.04 and use this Quick Start guide to set up your environment.
-
Install the latest MongoDB server compatible with Ubuntu 20.04 using the How to Install MongoDB on Ubuntu 20.04 guide.
-
Secure MongoDB on Ubuntu 20.04 to prevent unauthorized access.
Step 1 – Set up PyMongo
In this step, you will install PyMongo, which is the recommended MongoDB driver in Python. As a collection of tools that work with MongoDB, PyMongo uses Python native syntax and interfaces to facilitate database requests.
To enable PyMongo, open the Ubuntu terminal and install it from the Python package index. It is recommended to install PyMongo in a virtual environment to isolate your Python projects. If you missed how to set up a virtual environment in prerequisites, please refer to this guide.
pip3 install pymongo
Copy the code
Pip3 refers to the Python3 version of the popular Python PIP package installer. Note that in a Python 3 virtual environment, you can use the command PIP instead of pip3.
Now, open the Python interpreter with the following command. The interpreter is a virtual machine that operates like a Unix shell, where you can interactively execute Python code.
python3
Copy the code
When you get output similar to the one below, you are in the interpreter.
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.
Copy the code
With the output successful, import PyMongo into the Python interpreter.
import pymongo
Copy the code
Using the import statement, you can access the PyMongo module and its code from the terminal. The import statement will run without raising an exception.
On the next line, import getPass.
from getpass import getpass
Copy the code
Getpass is a module that manages password entry. The module prompts you for a password without showing it, and adds a security mechanism to prevent it from being shown in clear text.
Here, set up a connection to MongoClient and enable the MongoDB instance of your database. MongoClient declare a variable client to hold the MongoClient instance, with host,username,password, and authMechanism as parameters.
client = pymongo.MongoClient('localhost', username='username', password=getpass('Password: '), authMechanism='SCRAM-SHA-256')
Copy the code
MongoClient needs four parameters to connect to MongoDB with authorization enabled.
- host– Host name of the server on which MongoDB is installed. Since Mongo is local in this case, use it
localhost
。 - Usernameandpassword – Authorization credentials created after authentication is enabled in MongoDB.
- authMechanism–
SCRAM-SHA-256
This is the default authentication mechanism supported by clusters with MongoDB 4.0 or later authentication configured.
Once you have established a client connection, you can now interact with your MongoDB instance.
Step 2 – Test the database and collection
In this step, you will become familiar with NoSQL concepts such as collections and documents that apply to MongoDB.
MongoDB supports managing multiple independent databases in a single MongoClient instance. You can use the property style to access or create databases on the MongoClient instance. Declare a variable DB and specify the new database as a property of the client.
db = client.workplace
Copy the code
In this case, the Workplace database keeps track of the employee records you will add, such as the employee’s name and role.
Next, create a collection. Like tables in a relational database, collections store a set of files in MongoDB. In your Python interpreter, create a collection of employees as an attribute of DB and assign it to a variable with the same name.
employees = db.employees
Copy the code
Create the employees collection as an attribute of DB and assign it to a variable with the same name.
** Note: ** In MongoDB, the creation of databases and collections is lazy. This means that none of the above code is 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 – Perform CRUD operations
In this step, you will perform CRUD operations to manipulate the 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 a JSON-like document, a dictionary is used. Create a sample employee record with the name and role attributes.
employee = {
"name": "Sammy"."role": "Developer"
}
Copy the code
As you can see, the syntax of a Python dictionary is very similar to that of a JSON document. PyMongo converts Python dictionaries into JSON documents for extensible data storage.
At this point, the employee record is inserted into the Employees collection.
employees.insert_one(employee)
Copy the code
The insert_one() method is called on the employees collection to provide the previously created employee record for insertion. A successful insert should return a successful output like the following.
Output<pymongo.results.InsertOneResult object at 0x7f8c5e3ed1c0>
Copy the code
Now verify that you have successfully inserted the employee record and collection. Do a query to find the employee you just created.
employees.find_one({"name": "Sammy"})
Copy the code
The find_one() method is called on the employees collection to return a matching document with a name query. This is useful when you only have one document, or you are interested in the first matching document.
The output should look like this.
Output{'_id': ObjectId('606ae5b2358ddf640da46894'), 'name': 'Sammy', 'role': 'Developer'}
Copy the code
** Note: ** When a document is inserted, if it does not already contain a unique key _ID, a unique key _ID is automatically added to the document.
If you need to modify an existing document, use the update_one() method. The update_one() method takes two arguments: query and update.
- query–
{"name": "Sammy"}
– PyMongo will use this query parameter to find documents with matching elements. - update–
{ "$set": {"role": "Technical Writer"} }
The -update parameter is implemented$set operator
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"}})Copy the code
A successful update will return an output similar to this.
Output<pymongo.results.UpdateResult object at 0x7f8c5e3eb940>
Copy the code
To delete a single document, use the delete_one() method. Delete_one () takes a query parameter specifying the document to delete. Execute the delete_one() method as an attribute of the Employees collection, with Sammy as the query parameter.
employees.delete_one({"name": "Sammy"})
Copy the code
This will remove your only entry in the Employees collection.
Output<pymongo.results.DeleteResult object at 0x7f8c5e3c8280>
Copy the code
Using the find_one() method again, it is clear that you have successfully deleted Sammy’s employee record since nothing is printed to the console.
employees.find_one({"name": "Sammy"})
Copy the code
Insert_one (),find_one(),update_one(), and delete_one() are good ways to perform CRUD operations with PyMongo in MongoDB.
conclusion
In this tutorial, you’ve explored how to set up and configure database-driven PyMongo to connect Python code to MongoDB, and to create, retrieve, update, and delete documents. Although the focus of this guide is on introductory concepts, PyMongo provides a more powerful and flexible way to work with MongoDB. For example, you can do bulk inserts, query more than one document, index queries, and so on.
For more information on MongoDB management, see How to Back up, Restore, and Migrate MongoDB Databases on Ubuntu 20.04 and How to Import and Export MongoDB Databases on Ubuntu 20.04.