Python

Python and MongoDB Tutorial

Hello in this tutorial, we will understand how to perform CRUD operations in MongoDB via Python programming.

1. Introduction

Let us first understand that what is Mongo database.

  • MongoDB is a high-performance NoSQL database where each database has collections that in turn have documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN)
  • The documents in MongoDB don’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

To connect with the Mongo database in python programming we have the pymongo module known to the developer world as this module adhere to the python database api specification

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

1.2 Setting up PyMongo Connector Python

Once the python is successfully installed on your system you can install the PyMongo using a simple pip command. You can fire the below command from the command prompt and it will successfully download the module from pypi.org and install it.

Installation command

pip install pymongo

1.3 Setting up Mongo database container

To start with the tutorial, I am hoping that you have the Mongo database up and running in your localhost environment. For easy setup, I will set it up in the docker environment. You can use the below docker-compose file to get the containers running in minutes and it will also create an initial database (i.e. organization). The file also consists of the mongo-express which you can use to visualize the data present inside the mongo database. The mongo-express will be available at the following url – http://localhost:9001/ in the browser.

stack.yml

services:
  mongo:
    container_name: docker-mongo
    image: mongo
    environment:
      MONGO_INITDB_DATABASE: organization
      MONGO_INITDB_ROOT_USERNAME: mongoadmin
      MONGO_INITDB_ROOT_PASSWORD: secret
    ports:
      - "27017:27017"
  express:
    container_name: docker-mongo-express
    image: mongo-express
    ports:
      - "9001:8081"
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongo
      - ME_CONFIG_MONGODB_PORT=27017
      - ME_CONFIG_MONGODB_ADMINUSERNAME=mongoadmin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=secret
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
    depends_on:
      - mongo
version: "3"

To execute this docker-compose file you can execute the below docker-compose command.

Docker command

-- start the containers
docker-compose -f stack.yml up -d

-- stop the containers
docker-compose -f stack.yml stop

-- remove the containers
docker-compose -f stack.yml rm -f

If everything goes well the containers would be started successfully as shown in Fig. 1. You can use the docker ps -a command to confirm that the containers are started successfully. For further information on docker basics, you can navigate to this tutorial.

python mongodb - containers on docker
Fig. 1: Mongo database and Express containers on Docker

2. Python and MongoDB Tutorial

For the pymongo python module to work we will supply the following attributes to the mongo database from the python application i.e.

  • username – Identity of the user to work with the mongo database
  • password – Credential of the identity user
  • host – Address of the database server. If running on localhost then you can either use localhost or 127.0.0.1
  • port – The port number. Default mongo database port is 27017

3. Code Example

Let us dive in with the programming stuff now.

3.1 Create a configuration file

Add the following code to the environment file wherein we will specify the connection and database details. You are free to change these details as per your configuration setup.

local.env

[MONGO]
username = mongoadmin
password = secret
host = localhost
port = 27017

3.2 Reading the configuration

Add the following code to the python script which will read the configuration file created above and return the config object to be used later while connecting to the mongo database. The script will import the configparser module for reading the configuration file. Remember to give the correct path where the local.env is created.

readdbconfig.py

import configparser


def read_config_params():
    # reading the env file
    config = configparser.ConfigParser()
    config.read('config/local.env')
    return config

3.3 Connecting to the database

Add the following code to the python script which will connect to the mongo database with the help of the pymongo module.

connecttomongo.py

# python mongodb

from pymongo import MongoClient

from readconfig import *


def connect():
    try:
        # method will read the env file and return the config object
        params = read_config_params()

        # reading the parameters from the config object
        usr = params.get('MONGO', 'username')
        pwd = params.get('MONGO', 'password')
        host = params.get('MONGO', 'host')
        port = params.get('MONGO', 'port')

        # connect to mongodb
        connection_string = 'mongodb://' + usr + ':' + pwd + '@' + host + ':' + port + '/'
        client = MongoClient(connection_string)

        return client
    except Exception as error:
        print(error)

3.4 Insert data into the collection

Add the following code to the python script which will insert the default employee-related data to the employees collection inside organization database. The python script will check if default data is present in the collection or not. If present it will skip the insert operation. If not the default data will be inserted.

insert.py

# python mongo

import json

from connecttomongo import *
from helper import *


def insert(client, dbname, cname):
    with open('config/employees.json') as f:
        employees = json.load(f)

    try:
        if client is not None:
            # database
            db = client[dbname]
            # collection
            coll = db[cname]

            if get_total_records(coll) > 0:
                print('Default data present. Skipping insert')
            else:
                # inserting the entire employees list in the collection
                coll.insert_many(employees)
                print('Data inserted successfully')
        else:
            print('Could not connect with the mongo database')
    except Exception as error:
        print(error)


# driver code
if __name__ == '__main__':
    # connect to mongodb and insert into the collection
    insert(connect(), 'organization', 'employees')

If everything goes well the following output will be shown in the IDE console.

Logs

Data inserted successfully

3.5 Get all data from the collection

Add the following code to the python script which will get all the employee-related data from the employees collection. The python script will check if any existing data is present in the collection or not. If present it will print the data on the console. If not the following message – No data present in collection will be printed on the console.

getall.py

# python mongo

from connecttomongo import *
from helper import *


def get_all(client, dbname, cname):
    try:
        if client is not None:
            # database
            db = client[dbname]
            # collection
            coll = db[cname]

            if get_records_count(coll, {}) > 0:
                # get all documents from the collection
                employees = coll.find()
                # print
                print_info(records=employees)
            else:
                print('No data present in the collection')
        else:
            print('Could not connect with the mongo database')
    except Exception as error:
        print(error)


# driver code
if __name__ == '__main__':
    # connect to mongodb and get all data from the collection
    get_all(connect(), 'organization', 'employees')

If everything goes well the following output will be shown in the IDE console.

Logs

Id = 1, Name = Evyn Pickrell, Email = epickrell0@weebly.com, Gender = Female, Phone = 915-448-7844
Id = 2, Name = Karilynn Dranfield, Email = kdranfield1@trellian.com, Gender = Female, Phone = 453-241-7680
Id = 3, Name = Rosy Cashell, Email = rcashell2@tripod.com, Gender = Female, Phone = 221-822-0204
Id = 4, Name = Clarine Zoephel, Email = czoephel3@clickbank.net, Gender = Female, Phone = 711-289-2919
Id = 5, Name = Cornelius Polycote, Email = cpolycote4@wordpress.com, Gender = Male, Phone = 468-865-6423

3.6 Update a record in the collection

Add the following code to the python script which will update an employee record into the collection. The python script will check if the employee is present in the collection. If present the record will be updated. If not it will print the following message – Employee id = 5 not found will be printed on the console.

update.py

# python mongo

from connecttomongo import *
from helper import *


def update_by_id(client, dbname, cname, key):
    try:
        if client is not None:
            # database
            db = client[dbname]
            # collection
            coll = db[cname]
            # search param
            search_filter = {'id': key}

            if get_records_count(coll, search_filter) > 0:
                coll.update_one(
                    {'id': key},
                    {
                        "$set": {
                            "first_name": "Mellicent",
                            "last_name": "Carillo",
                            "email": "mcarillo4@tumblr.com",
                            "gender": "Non-binary",
                            "phone": "969-694-0483"
                        }
                    }
                )
                print('Employee id = {} updated successfully'.format(key))
            else:
                print('Employee id = {} not found'.format(key))
        else:
            print('Could not connect with the mongo database')
    except Exception as error:
        print(error)


# driver code
if __name__ == '__main__':
    # connect to mongodb and update by id in collection
    update_by_id(connect(), 'organization', 'employees', 5)

If everything goes well the following output will be shown in the IDE console. You can run the getall.py python script to fetch the records.

Logs

Employee id = 5 updated successfully

3.7 Delete a record in the collection

Add the following code to the python script which will delete an employee record into the collection. The python script will check if the employee is present in the collection. If present the record will be deleted. If not it will print the following message – Employee id = 5 not found will be printed on the console.

delete.py

# python mongo

from connecttomongo import *
from helper import *


def delete_by_id(client, dbname, cname, key):
    try:
        if client is not None:
            # database
            db = client[dbname]
            # collection
            coll = db[cname]
            # search param
            search_filter = {'id': key}

            if get_records_count(coll, search_filter) > 0:
                coll.delete_one(search_filter)
                print('Employee id = {} deleted successfully'.format(key))
            else:
                print('Employee id = {} not found'.format(key))
        else:
            print('Could not connect with the mongo database')
    except Exception as error:
        print(error)


# driver code
if __name__ == '__main__':
    # connect to mongodb and update by id in collection
    delete_by_id(connect(), 'organization', 'employees', 5)

If everything goes well the following output will be shown in the IDE console. You can run the getall.py python script to fetch the records.

Logs

Employee id = 5 deleted successfully

Similarly, we can create other scripts like deleteall.py to delete all records from the employees collection and many others. That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

4. Summary

In this tutorial, we learned:

  • The pymongo module
  • Sample programs to perform CRUD operations using Python and MongoDB.

You can download the source code of this tutorial from the Downloads section.

5. Download the Project

This was a python programming tutorial to connect to MongoDB with the help of pymongo python module and perform CRUD operations.

Download
You can download the full source code of this example here: Python and MongoDB Tutorial

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button