Skip to content

#14 Interact MongoDB with Python

In addition to utilizing Mongo Compass or Mongo Shell, another effective means of interacting with MongoDB is through the use of Drivers.In this article, I will show you how to connect MongoDB with python and interact with the database.

Step 1: Install the pymongo package

pip3 install pymongo

Step 2: Build the connection in Python

Firstly, we need to load the pymongo libaray and then we will build a connection with MongoClient().

After that, we need to refer the database and collection that we will use.

from pymongo import MongoClient                        # load the library

client = MongoClient('mongodb://localhost:27017/')      # build the connection

db = client.company                                    # refer the database
personnel = db.personnel                               # refer the collection

Step 3: Interact with Database using Python

We can use the pythonic method to interact the database.

Example 1: select only name and age of each document

# select only name and age of each document
for person in personnel.find({},{'name':1,'age':1}):
   print(person)
  
{'_id': ObjectId('66053f848ccb6407b4ba21c4'), 'name': 'John Doe', 'age': 30}
{'_id': ObjectId('66053f848ccb6407b4ba21c5'), 'name': 'Jane Smith', 'age': 28}
{'_id': ObjectId('66053f848ccb6407b4ba21c6'), 'name': 'Michael Johnson', 'age': 35}
{'_id': ObjectId('66053f848ccb6407b4ba21c7'), 'name': 'Emily Brown', 'age': 32}
{'_id': ObjectId('66053f848ccb6407b4ba21c8'), 'name': 'David Wilson', 'age': 40}
{'_id': ObjectId('66053f848ccb6407b4ba21c9'), 'name': 'Sarah Lee', 'age': 27}
{'_id': ObjectId('66053f848ccb6407b4ba21ca'), 'name': 'Alex Garcia', 'age': 34}
{'_id': ObjectId('66053f848ccb6407b4ba21cb'), 'name': 'Michelle Martinez', 'age': 31}
{'_id': ObjectId('66053f848ccb6407b4ba21cc'), 'name': 'Daniel Thompson', 'age': 29}
{'_id': ObjectId('66053f848ccb6407b4ba21cd'), 'name': 'Amanda Hall', 'age': 33}

Example 2: find documents whose salary is greater than 68000

# find documents whose salary is greater than 68000
print([p for p in personnel.find({'salary':{'$gt':68000}})])

[{'_id': ObjectId('66053f848ccb6407b4ba21c6'), 'name': 'Michael Johnson', 'age': 35, 'working_experience': 8, 'department': 'Finance', 'salary': 70000, 'skills': ['Accounting', 'Financial Analysis', 'Excel']}, 
{'_id': ObjectId('66053f848ccb6407b4ba21c8'), 'name': 'David Wilson', 'age': 40, 'working_experience': 12, 'department': 'Operations', 'salary': 75000, 'skills': ['Project Management', 'Supply Chain', 'Lean Six Sigma']}, 
{'_id': ObjectId('66053f848ccb6407b4ba21cd'), 'name': 'Amanda Hall', 'age': 33, 'working_experience': 9, 'department': 'Quality Assurance', 'salary': 69000, 'skills': ['Testing', 'Bug Tracking', 'Automation']}]

Example 3: Use Pipeline to write complex query and group data

from bson.son import SON

pipeline = [
    {
        "$group":{                                #group data by deparment
            "_id": "$department",
            "meanSalary": {"$avg": "$salary"}     #calculate the avg salary
        }
    },
    {
        "$sort": SON([("meanSalary",-1)])        # sort data by meanSalry in DESC
    }
]

results = personnel.aggregate(pipeline)

for result in results:
    print(result)

------------------------------------------------

{'_id': 'Operations', 'meanSalary': 75000.0}
{'_id': 'Finance', 'meanSalary': 70000.0}
{'_id': 'Quality Assurance', 'meanSalary': 69000.0}
{'_id': 'Sales', 'meanSalary': 67000.0}
{'_id': 'Research and Development', 'meanSalary': 63000.0}
{'_id': 'Human Resources', 'meanSalary': 62000.0}
{'_id': 'Engineering', 'meanSalary': 60000.0}
{'_id': 'Product Management', 'meanSalary': 58000.0}
{'_id': 'Marketing', 'meanSalary': 55000.0}
{'_id': 'Customer Support', 'meanSalary': 50000.0}