Select Page

Python Database Programming with MongoDB

Phil Hajjar
Published: May 7, 2022

The purpose of this Python database programming tutorial is to provide a gentle introduction to using a NoSQL database as a data store for Python applications. The intended audience is beginner-level developers or intermediate-level developers who are familiar with using SQL-oriented databases such as Oracle, SQL Server, or MySQL. The demonstrations in this article will parallel the demonstrations used in other SQL Database programming articles involving Python, such as Python Database Programming with SQL Express for Beginners, and likewise, will use Python 3.

What is NoSQL?

Python’s popularity continues to grow in popularity in part because of the ease of which it can work with new technologies. One such “new” technology is the use of “NoSQL” databases. One of the most appealing features of these types of databases is the fact that there is, as the name implies, generally no additional query language, such as one of seemingly many variants of SQL, that must be learned in order to work with the database from a programmatic standpoint. This greatly eases application development because the native data structures of the programming language and its syntax are the de facto “query language,” provided of course the appropriate modules are added to the development environment.

NoSQL databases, in addition to mostly not using a query language, also structure data internally in a manner that is significantly dissimilar to SQL databases. A developer who is more used to the SQL programming paradigm must adapt his or her thinking to match how NoSQL does business. Going further under the hood, the design of NoSQL database servers shares the same dissimilarity. NoSQL database servers are more focused on speed and scalability with “eventual consistency”, as opposed to SQL database servers which are focused more on immediate consistency.

What is MongoDB?

MongoDB is one of many “NoSQL” databases. Like MariaDB, it uses its own username and password system for access management. MongoDB, in addition to offering zero-cost downloadable server software, also offers a zero-cost cloud-based introductory option (via its Atlas offering) which mitigates the need for downloading and configuring an instance. The demonstrations in this database programming tutorial will use the cloud-hosted free instance of MongoDB.

As is the case with Python and any database server, an additional database driver module, namely PyMongo, is required for Python to communicate with the MongoDB server and any databases contained within it. Furthermore, like any database server, MongoDB provides a companion management tool called MongoDB Shell. The demonstrations in this article will use a MongoDB database created using this tool. Note that MongoDB Shell is one of many management tools that exist for MongoDB.

Please note that the links posted were active at the time this article was written. If the links are no longer active, these products can be downloaded via one’s preferred search engine.

How to Configure MongoDB for Software Development

The MongoDB Zero-Cost Cloud-Based Introductory Offering configuration is entirely web-based. It can conveniently be linked to one’s Google account for the purposes of single-sign-on. Just bear in mind the following caveats:

  • Choose a data hosting service that is physically nearby. The hosting service shown in this article which is in Virginia may not be ideal for someone who is far away from there.
  • The password that is used to connect an application to the database server must be saved securely as it cannot be recovered if it is lost. It can only be reset.
  • The Cluster Name, once set, cannot be changed. The demonstrations in this article will use a cluster named Cluster0.
  • Ensure that any IP address from which any application, including MongoDB Shell, is configured in the security settings.
  • The MongoDB Shell may not have an installation program. Always make sure to include the path to the folder which contains this application in the Operating System path.

How to Connect to the MongoDB Database Server

Upon successfully configuring the Cloud-Based MongoDB Cluster, it will be necessary to note the particular connection string used to connect to it. The strings below can also be retrieved by logging into MongoDB Atlas Site and clicking the Connect button:

Database Deployment MongoDB Atlas

Figure 1 – the MongoDB Atlas Dashboard with the Connect Button Highlighted

Note, settings such as the database username and password, and allowed IP ranges for connection can be set in the Database Access and Network Access links, respectively.

Below are two connection examples. The first is for MongoDB Shell.

MongoDB: Connecting from the Command Line

Opening a Windows Command Prompt is explained in Python Database Programming with SQL Express for Beginners under the Opening Command Prompt Windows heading.

MongoDB Python Connection

Figure 2 – Connecting via the Command Line to MongoDB Shell

Note that the command to open the MongoDB Shell will vary by Operating System. The Cluster name may vary, and the highlighted parameters in the command will need to be changed to the particulars of the cloud-based MongoDB setup. In Windows, this command is called mongosh. Note that the database name RazorDemo is included below, as that is the database that will be used in the demonstrations in this programming tutorial. The command also accepts an additional parameter for the password as shown below:

MongoDB Authentication

Figure 3 – Authenticating with the Password in the Command-Line.

Note, while it is convenient for development purposes to use the password in the command line, as opposed to having to type it in each time, be mindful that the password shows up in the history of previously executed commands. This option should not be used in development environments where other users can access one’s previous command history.

Also, hopefully, nobody thought that I would actually use 123456 as a password, right?

Authentication with MongoDB and Python

Figure 4 – Just Checking 🙂

Note that in both examples above, the ^ character is used as a line continuation symbol in the Windows Command Prompt.

Connecting MongoDB from Python

The following string, with suitable changes, can be placed into Python code for use by the PyMongo driver:

Connect Python and MongoDB

Figure 5 – Connecting via Python and PyMongo

In the connection string above, the database name RazorDemo will be used in place of myFirstDatabase.

How to Create a Database in MongoDB

MongoDB does not “create” a database until that database is “used” and data is actually written into it. The image below shows a successful connection to the MongoDB Atlas service using the RazorDemo database, but there is no such database listed when using the show dbs command:

MongoDB Razor Pages

Figure 6 – No RazorDemo database… yet

How to “Use” a Database in MongoDB

In MongoDB, a database is “used” whenever it is either connected to directly using the query string or when the database name is the argument of the use command. Note how invoking this command on the currently selected database displays a different message than switching to that database:

MongoDB Switch Database

Figure 7 – Switching between databases

To actually “create” the database, some data must be inserted. The RazorDemo database from the previous articles, Getting Started With Razor Pages Using C# and Python Database Programming with SQL Express for Beginners, uses two tables, namely Artists and Albums. In MongoDB, what an SQL-oriented database calls a table is referred to as a collection. Creating two entries into collections named Artists and Albums, respectively, will create both the database as well the collections.

Each entry in a collection is referred to as a document. This is analogous to a record in a SQL-oriented database table.

Note, just as it is important for a major application to save the create table statements in a SQL-oriented database, the same reasoning will also apply to the initial database and collection creation commands for a MongoDB database for a major application.

The commands below will create the Artists collection by inserting a single document into it, and then query the Artists collection to verify the success of the insert operation. Note how the insert operation uses JSON syntax:

db.Artists.insertOne({artist_name: "Skeered Tigon Stripes"})

db.Artists.find({})

Listing 1 - Inserting a new document.

These commands give the following output in MongoDB Shell:

Collections in MongoDB

Figure 8 – Creating the Artists collection

The _id shown above was automatically created and is unique within the collection, similar to an automatically generated primary key value in a SQL-oriented database.

Note, MongoDB Shell may report that the .insert() command is deprecated. This example uses the .insertOne() command as that is one of the provided alternatives.

Similar syntax can be used to create the Albums collection:

db.Albums.insertOne({artist_name: "Skeered Tigon Stripes",album_name: "Genetically Engineered Critters"})

db.Albums.find({})

Listing 2 - Inserting another new document

MongoDB Collection Creation

Figure 9 – Creating the Albums collection

Note, The warning related to caniuse-lite appears when pressing the Enter key before completing the command to create a document. MongoDB Shell will allow for a command to be broken across multiple lines before completing it. This warning indicates that MongoDB Shell may be using an outdated driver and it will need to be updated in a future release.

It does not appear to break the document creation process.

Some more documents for the Albums collection:

db.Albums.insertOne({ artist_name: "Skeered Tigon Stripes", album_name: "Volume Control: No" })
db.Albums.insertOne({ artist_name: "Skeered Tigon Stripes", album_name: "Verse Vica })
db.Albums.insertOne({ artist_name: "Skeered Tigon Stripes", album_name: "Rotten to the Core" })

db.Albums.find({})

Listing 3 - Additional documents for the Albums collection

MongoDB Collection Example

Figure 10 – New additional Album documents

NO Foreign Key Constraints

As MongoDB is not a relational database management system, it does not provide a mechanism for enforcing the kinds of foreign key constraints used in SQL-oriented database tables between collections. It expects the application to maintain and enforce such relations. In the context of MongoDB Shell, it is incumbent upon the developer to properly type in the values to ensure that a relationship between the documents in the collections can be inferred.

The .deleteOne() command can be used to delete incorrectly entered documents:

db.Albums.deleteOne({ artist_name: "Skeered Tigon Stripes", album_name: "I Typed the Album Name Wrong" })
Listing 4 - Deleting a document.

Note that, if a document does not match the parameters entered, no error will be displayed, instead, the confirmation message will show that nothing was deleted:

Python MongoDB tutorial

Figure 11 – Confirmation message showing no documents deleted

The .deleteMany() command can be used to delete multiple records which match a JSON criteria. The command below deletes all documents in the Albums collections in which the artist_name value is not Skeered Tigon Stripes:

db.Albums.deleteMany({artist_name: {$ne : "Skeered Tigon Stripes" }})

Listing 5 - Deleting multiple documents based on criteria.

MongoDB Database example

Figure 12 – Deleting multiple records

Final Thoughts on Python Database Programming with MongoDB

Now that we have learned how to connect to a MongoDB database and how to create a NO SQL version of a table with “records”, we can move on to working with Python scripts to insert data into our MongoDB database. We will continue that discussion in the second – and final – part of this Python database programming tutorial: Python and MongoDB Database Development.

Source: www.developer.com