DEV Community

dev0928
dev0928

Posted on

Working with MongoDB and Python

MongoDB is a NoSQL, general purpose, document-based, distributed database. It stores data in a familiar JSON like format which many developers are comfortable working with. In this article, let’s take a look on how to get started working with MongoDB in a Python environment by performing basic Create, Read, Update and Delete (CRUD) operations.

Why MongoDB?

Traditional Relational Database Systems (RDBMS) have a steep learning curve as one has to get comfortable with database concepts and SQL. Since MongoDB has no strict schema requirements, it is relatively very easy to get started. Here is a comparison between NoSQL and RDBMS.

Basic MongoDB terms

In order to use MongoDB, one needs to understand basic terms used by MongoDB. MongoDB arranges data by document. So, a row of data in MongoDB is called a document. In RDBMS, tables contain number of rows while in MongoDB tables are called collections as they contain a group of documents.

Below table lists basic relational database terms along with its equivalent in MongoDB:

RDBMS Concept MongoDB Equivalent
Database Database
Tables Collections
Rows Documents
Columns Fields
Index Index

How to install MongoDB?

Installing MongoDB using docker is straight forward. Here are the commands to install using docker.

docker pull mongo
#Running Mongo as a background task with data mapped to external home/mongo/data folder 
docker run -d mongo -v /home/mongo/data:/data/db --name mongodb
Enter fullscreen mode Exit fullscreen mode

This link contains installation instructions for other environments.

How to work with MongoDB in Python?

To work with MongoDB in Python, one needs to install PyMongo driver using command - python -m pip install pymongo

How to Create a Collection in MongoDB?

Below file connects to MongoDB installed in the local environment and creates a database called booksdb containing books collection.

from pymongo import MongoClient

#Step 1: Connect to MongoDB - Change connection string as needed
client = MongoClient(port=27017)
db=client.booksdb

#Step 2: Prepare sample data
book_list = ["Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
            "Python for Everybody: Exploring Data in Python 3",
            "Dive Into Python 3",
            "Test Book"]
author_list = ["Al Sweigart",
            "Dr. Charles Russell Severance, Sue Blumenberg, Elliott Hauser, Aimee Andrion",
                "Mark Pilgrim",
                "Test Author"]
publication_year_list = [2015, 2016, 2012, 2020]

#Step 3: Insert sample data
for idx in range(0, 4):
    book = {
        'title' : book_list[idx],
        'author' : author_list[idx],
        'pub_year' : publication_year_list[idx]
    }
    #Step 4: Insert book object by using MongoDB’s isnert_one
    result=db.books.insert_one(book)
    #Step 5: Print ObjectID of the new document
    print(f'Created {idx+1} of 4 as {result.inserted_id}')

print('Finished creating books')
Enter fullscreen mode Exit fullscreen mode

Please note MongoDB automatically adds a _id field with unique id value to every document during document creation.

How to query Collections in MongoDB?

Below listing shows how to query a collection matching a criteria and query all documents in a collection.

from pymongo import MongoClient
from pprint import pprint

#Connect to MongoDB
client = MongoClient(port=27017)
db=client.booksdb

# Print one of the books matching criteria
book = db.books.find_one( { 'pub_year': 2020} )
pprint(book)

# Print all of the books
books = db.books.find( { } )
for bk in books:
    pprint(bk)
Enter fullscreen mode Exit fullscreen mode

How to update Collections in MongoDB?

Unlike traditional databases, new fields could be added to a particular row of data. Below listing adds the copies_sold column to a book document with publication year of 2015.

from pymongo import MongoClient
from pprint import pprint

#Connect to MongoDB
client = MongoClient(port=27017)
db=client.booksdb

#Find document
book = db.books.find_one( { 'pub_year': 2015} )
pprint(book)

#Update document
result = db.books.update_one({'_id' : book.get('_id') }, {'$inc': {'copies_sold': 50}})
print('Number of documents modified : ' + str(result.modified_count))

#Print updated document
updated_book = db.books.find_one({'_id':book.get('_id')})
print('The updated document:')
pprint(updated_book)
Enter fullscreen mode Exit fullscreen mode

How to delete documents in MongoDB?

Here is a sample listing containing document deletion matching a certain criteria.

from pymongo import MongoClient

#Connect to MongoDB
client = MongoClient(port=27017)
db=client.booksdb

#Delete document matching sample criteria
result = db.books.delete_many({"title": "Test Book"})

#Print deletion count
print(f'Deleted {result.deleted_count} documents!')
Enter fullscreen mode Exit fullscreen mode

Further Reading

To know more about MongoDB please visit MongoDB documentation.

Top comments (2)

Collapse
 
ktkaushik profile image
Kaushik Thirthappa

Next up - aggregate in mongodb?

Good write up 👏

Collapse
 
nyangweso profile image
Rodgers Nyangweso

thanks for sharing, quite helpful