Introduction

NoSQL databases represent a shift from traditional relational database management systems (RDBMS) and include a range of database technologies that are designed to handle large data sets, provide flexibility in dealing with data models, and ensure scalability and high availability.

While SQL databases use structured query language (SQL) for defining and manipulating data, NoSQL databases have a dynamic schema for storing unstructured data, including key-value pairs, wide-column stores, graph databases, or document databases.

In this article, we will explore the importance and advantages of NoSQL databases. You will see a hands-on example of how to interact with MongoDB, a popular NoSQL database, using Python. The hands-on section will cover basic CRUD (Create, Read, Update, and Delete) operations, giving you a solid understanding of working with NoSQL databases.

Why Use NoSQL Databases

As the digital world experiences an explosion in the volume of data, NoSQL databases have gained prominence due to their specific capabilities, some of which are mentioned below:

  • Efficiently handles large volumes of structured, semi-structured, and unstructured data.
  • Highly flexible, allowing rapid application data model changes without a predefined schema.
  • Scalable by easily adding more servers to handle increased traffic.
  • High performance for read and write operations in high-scale applications.
  • Cost-effective solution for handling massive datasets with built-in fault tolerance.

Commonly Used NoSQL databases

Various NoSQL databases are in use today, each designed to meet specific needs. Here are some of the most commonly used NoSQL databases.

MongoDB:

  • Document-oriented database with JSON-like documents for dynamic schema and data representation.
  • Scalable and high-performance, supporting horizontal scaling and geospatial indexing.
  • Rich query language for complex queries and efficient indexing for faster retrieval.

Cassandra:

  • Column-family database with flexible data modeling.
  • Distributed and fault-tolerant, designed for high availability across multiple nodes and data centers.
  • Linear scalability, capable of handling large datasets with ease.

Redis:

  • In-memory data store for lightning-fast read and write operations.
  • Supports various data structures like strings, lists, sets, hashes, and sorted sets.
  • Implements Pub/Sub messaging for real-time data dissemination.

Neo4j:

  • Graph database optimized for managing interconnected data.
  • Allows complex graph traversal and pattern matching queries.
  • Supports ACID transactions and comes with built-in graph algorithms.

Amazon DynamoDB:

  • Fully managed database offered as a cloud service.
  • Supports key-value and document data model for flexible data storage.
  • Predictable performance with low-latency and global replication for improved availability.

In the next section, we will provide an example of how to perform CRUD (Create, Read, Update, and Delete) operations on MongoDB using Python.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Performing CRUD Operations on MongoDB in Python

To perform CRUD operations on MongoDB, you first need to install the MongoDB database on your system and download the Python PyMongo library.

Installing MongoDB

For detailed installation instructions based on your specific operating system (Windows, Linux, or MacOS), you can refer to the official MongoDB installation guide since the installation instructions below may change over time.

mongodb download options

Select the community edition for your specific platform. For example, if you are using Windows, choose the Windows Community Edition. For other platforms, choose their community edition.

Click the “MongoDB Download Center” option on the following screenshot. Select the version, platform, and package for your operating system.

mongodb download center

Click the “Download” button in the following window to download MongoDB.

mongodb download button

After the download completes, open the setup file and keep clicking the “Next” button until you get to a screenshot like following. Once you get to that screen, select “Complete.”

mongodb download complete

Keep the default settings on the following window, then click “Next”.

mongodb download service config

Finally, click the “Install” button on the following window to install MongoDB.

mongodb download installation


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Connecting MongoDB with Python

To allow Python to interact with MongoDB, you need a MongoDB driver. One of the most common ones is the PyMongo package, which can be installed via pip (Python’s package manager). In your terminal or command prompt, type:

pip install pymongo

To connect Python with MongoDB, you need to import the MongoClient class from the pymongo module. Create an object of the MongoClient class and pass it the path to your MongoDB listening port which, by default, is mongodb://localhost:27017/. If your connection is successful, the following script should print “Connected to MongoDB”.

from pymongo import MongoClient

try:
    client = MongoClient('mongodb://localhost:27017/')
    print("Connected to MongoDB")
except Exception as e:
    print("Failed to connect to MongoDB")
    print(e)

Output:

Connected to MongoDB

Creating a Dummy Databases

Let’s create a dummy database that we will use as our basis for executing CRUD operations.

The following script first checks if a database named testDB exists. If not, it creates one. In MongoDB, a database isn’t created until you insert some data into it.

dblist = client.list_database_names()
if "testDB" in dblist:
    print("The database exists.")
else:
    print("Creating database...")

db = client['testDB']

Output:

Creating database...

Creating a Collection

Next, we create a collection named testCollection in the testDB database we just created. A collection in MongoDB is similar to tables in SQL databases and is used to store data.

collection = db['testCollection']
print("Collection 'testCollection' created.")

Output:

Collection 'testCollection' created.

Inserting a Document

The insert_many() method of the collection object inserts multiple documents (or records) in a collection. A document is a dictionary that contains key-value pairs.

The following script inserts multiple documents into testCollection collection. If the documents are inserted successfully, IDs of the documents are printed. Otherwise, an exception is handled and an error message is displayed.

records = [
    {"name": "John", "age": 30, "city": "New York"},
    {"name": "Jane", "age": 28, "city": "Chicago"},
    {"name": "Doe", "age": 26, "city": "San Francisco"}
]
try:
    insert_result = collection.insert_many(records)
    print(f"Records inserted with ids {insert_result.inserted_ids}")
except Exception as e:
    print("Failed to insert records")
    print(e)

Output:

Records inserted with ids [ObjectId('64c3e5a5e231bbe57df41155'), ObjectId('64c3e5a5e231bbe57df41156'), ObjectId('64c3e5a5e231bbe57df41157')]

Read All Records

The find() method of the collection object returns all the documents (or records) in a collection.

print("\nReading all records:")
results = collection.find()
for record in results:
    print(record)

Output:

Reading all records:
{'_id': ObjectId('64c3e5a5e231bbe57df41155'), 'name': 'John', 'age': 30, 'city': 'New York'}
{'_id': ObjectId('64c3e5a5e231bbe57df41156'), 'name': 'Jane', 'age': 28, 'city': 'Chicago'}
{'_id': ObjectId('64c3e5a5e231bbe57df41157'), 'name': 'Doe', 'age': 26, 'city': 'San Francisco'}

Update a Record

The update_one() method of the collection object updates a single record in a collection. The method accepts query and new value dictionaries as parameters. The query dictionary consists of the criteria for selecting the record you want to update. The new values dictionary consists of the new values you want to update in your records. They will replace the old values. In the following script, the age of John is updated.

print("\nUpdating a record:")
query = {"name": "John"}
new_values = {"$set": {"age": 35}}
try:
    update_result = collection.update_one(query, new_values)
    print(f"Records matched: {update_result.matched_count}")
    print(f"Records modified: {update_result.modified_count}")
except Exception as e:
    print("Failed to update record")
    print(e)

##Read all records again to see the updated record

print("\nReading all records after update:")
results = collection.find()
for record in results:
    print(record)

Output:

Updating a record:
Records matched: 1
Records modified: 1

Reading all records after update:
{'_id': ObjectId('64c3e5a5e231bbe57df41155'), 'name': 'John', 'age': 35, 'city': 'New York'}
{'_id': ObjectId('64c3e5a5e231bbe57df41156'), 'name': 'Jane', 'age': 28, 'city': 'Chicago'}
{'_id': ObjectId('64c3e5a5e231bbe57df41157'), 'name': 'Doe', 'age': 26, 'city': 'San Francisco'}

Deleting a Record

The delete_one() method deletes a single record from a collection. You simply need to pass it the query dictionary containing the record you want to delete.

#Delete a record
print("\nDeleting a record:")
query = {"name": "John"}
try:
    delete_result = collection.delete_one(query)
    print(f"Records deleted: {delete_result.deleted_count}")
except Exception as e:
    print("Failed to delete record")
    print(e)

#Read all records again to see if the record is deleted
print("\nReading all records after delete:")
results = collection.find()
for record in results:
    print(record)

Output:

Deleting a record:
Records deleted: 1

Reading all records after delete:
{'_id': ObjectId('64c3e5a5e231bbe57df41156'), 'name': 'Jane', 'age': 28, 'city': 'Chicago'}
{'_id': ObjectId('64c3e5a5e231bbe57df41157'), 'name': 'Doe', 'age': 26, 'city': 'San Francisco'}

You now know how to perform CRUD operations on a NoSQL database with Python! This opens up a whole host of programming possibilities so get creative!


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.