MongoDB with python

MongoDB with python complete tutorial | Learn pymongo in 2022

MongoDB is one of the most popular No SQL databases which can handle a high volume of data. In this blog, we will learn about the MongoDB python driver and understand how to use MongoDB with python.

We can interact with MongoDB using the mongo command-line interface. Apart from the native command-line interface, MongoDB also provides various drivers to connect with our MongoDB instance. Besides the python driver, MongoDB also provides the driver for other programming languages. The list of drivers can be found here.

How to connect MongoDB with python

Python is the first choice of data scientists and data engineers when it comes to language selection. This is because of the vast community support and multiple available packages.

And MongoDB is one of the most popular No SQL databases which can be used to build web applications, JSON APIs, etc.

To connect MongoDB with python, we can use the pymongo driver. It is the official driver provided by MongoDB.

What is Pymongo

Pymongo is the official driver provided by the MongoDB team. It’s a python package to interact with mongo shell. With the help of the pymongo driver, we can connect MongoDB with python.

With the help of a pymongo driver, you can perform various CRUD operations of MongoDB.In the letter session of the course, we will see examples of various CRUD operations.

Now it is time to get started with the pymongo installation

Install pymongo

Installing pymongo is relatively straightforward. You can install pymongo with simple pip command.

To get started with pymongo, you should have a running MongoDB instance. You can follow my tutorial on getting started with MongoDB to install MongoDB in a docker container.

Once you have a working MongoDB instance, you can connect to MongoDB docker containers and type the below command to install pymongo.

apt-get update
apt-get install python3
apt-get install python3-pip
pip install ipython
pip install pymongo
Collecting pymongo
  Downloading pymongo-3.12.0-cp38-cp38-manylinux2014_x86_64.whl (545 kB)
     |████████████████████████████████| 545 kB 1.6 MB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.12.0

Type ipython to connect to interactive python shell in MongoDB docker container and import pymongo package to see if the installation is working fine

If the pymongo packages get successfully imported without any error, it means your pymongo installation is OK.

Connect mongodb with python

To connect to MongoDB using python, we need to import the pymongo package

import pymongo

Now the next step is the create a MongoClient.The MongoClient takes the MongoDB connection string as a parameter.

Since our MongoDB is running locally on port 27017, we use the below connection string to connect to MongoDB.

client = pymongo.MongoClient("mongodb://localhost:27017/")

If the above commands run without any error, it means the connection to MongoDB is successful. Let’s proceed further and see some of the useful MongoDB commands.

Python mongodb connection with username and password

If you have set up a MongoDB instance that requires a username and password to authenticate, then you can use the below code snippet to connect to the MongoDB instance.

import pymongo
client = pymongo.MongoClient("mongodb://[username:password@]localhost:27017/")

Python MongoDB query

pymongo commands

Python MongoDB queries are very similar to the ones used by the mongo command-line interface. If you are not familiar with that command, you can check out my blog about MongoDB tutorials for beginners before proceeding further.

Pymongo create database and collections

This session will help you to create a database and collection in MongoDB.MongoDB collections are similar to tables in a relational database.

Pymongo create a database

Before creating a database in MongoDB, a mongo client needs to be created. Once the client gets to create, type the below command to create a mongo database

db = client['naiveskill']

Pymongo create a collection

Once the DB gets created, type the below command to create a collection called myCollection.

collection = db['myCollection']

NOTE: Collections will not be visible in MongoDB until or unless a document is inserted.

Pymongo Insert Command

With MongoDB insert command, we can insert documents into the MongoDB.We have the flexibility to insert a single document or multiple documents simultaneously

Pymongo insert one

with the insert_one command; you can insert a single document into MongoDB.The insert_one command takes input in the form of a dictionary.

Type the below command to insert a single document.

dict1= {'job': 'softare engineer' , 'Grade':7}

collection.insert_one(dict1)
pymongo insert one

NOTE:

  • The Mongo database will not be visible until the collection has been created and at least one document has been inserted into the mongo collection.
  • _id field will be added by mongodb to uniquely identify records.
  • You can also set the _id by yourself if needed.

Pymongo insert many

We can insert more than one document in MongoDB with the insert many commands in one go. Insert many takes list of the dictionary as a parameter.

Type the below command to insert 4 documents in mongodb

dict_many= [
	{'job': 'softare engineer' , 'Grade':7},
	{'job': 'Data engineer' , 'Grade':6},
	{'job': 'dataops engineer' , 'Grade':8},
	{'job': 'mlops engineer' , 'Grade':7},
	]

In [12]: collecton.insert_many(dict_many)
pymongo insert many

Pymongo list database and collection

Pymongo list database

Type the below command to get all the databases

client.list_database_names()
pymongo list database

Pymongo list collection

Type the below command to list all collections in the naiveskill database

client['naiveskill'].list_collection_names()
pymongo list collection

Pymongo find command

Pymongo find one

Find_one command returns a single document.

collection.find_one()
pymongo find_one

Pymongo find one with a filter condition

We can also add a filter condition in the find_one command. Type the below command to filter out the documents where job=dataops engineer

collection.find_one({'job': 'dataops engineer'})
pymongo find_one

Pymongo find all

Find all command returns all the documents present in the given collection.

collection.find()

To print the o/p in the command line, you have to loop through the document.

pymongo find

Pymongo Less than and equal to along with find command

While finding the document in MongoDB, we can also have the flexibility to pass greater than, less than, greater than equal to, and less than equal to expressions.

Pymongo greater than

To filter out the documents where the Grade is greater than 7.

collection.find({'Grade': {"$gt": 7}})
pymongo find with greator than condition
Pymongo less than

Type the below command to filter out the documents where the Grade is less than 8.

collection.find({'Grade': {"$lt": 8}})
pymongo find with less than condition

Pymongo find few columns from a document

In the find command, we can also specify the columns we do not want to print in the o/p.

Column_name: 0 means do not print this field

Column_name: 1 means print this field

Type the below command to exclude the _id field.

collection.find({'Grade': 7}, {'_id':0})
pymongo find

In case you only need the job to be displayed in documents.

collection.find({'Grade': 7}, {'job':1 ,'_id':0})
pymongo find few columns

Pymongo count all documents

There are many ways to get the count of records from a mongo collection. You can either use count() or estimated_document_count().

collection.count()

collection.estimated_document_count()
pymongo collection count

Pymongo update Command

With the pymongo update command, you can easily update the document’s content. Similar to the pymongo find command, we have updated one and updated many commands.

Pymongo update one

It is used to update a single document.

If you have to observe while inserting a document into the MongoDB collection, we have made a typo and inserted ‘job’: ‘softare engineer’ instead of ‘job’: ‘software engineer’ .Type the below command to fix that typo error.

update one records where job=softare engineer

pre_value= {'job': 'softare engineer'}
new_value = {"$set": {'job': 'software engineer'}}
collection.update_one(pre_value, new_value)
pymongo update_one

Verify if the records get updated

pymongo update_one

Pymongo update many

With update many, we can update multiple documents. Type the below command to update all instances of ‘job’: ‘softare engineer’.

pre_value= {'job': 'softare engineer'}
new_value = {"$set": {'job': 'software engineer'}}
collection.update_many(pre_value, new_value)
pymongo update_many

Pymongo delete Command

With Pymongo delete command, you can delete a document from the MongoDB collection. Similar to insert, we have to delete one and delete many commands.

Pymongo delete one

Deletes a document from the MongoDB collection.

Type the below command to delete a single document where {‘job’: ‘mlops engineer’}

rec= {'job': 'mlops engineer'}
collection.delete_one(rec)
pymongo delete_one

Pymongo delete many

Deletes all documents from the MongoDB collection based on a filter condition.

Type the below command to delete all documents where {‘job’: ‘mlops engineer’}

rec= {'job': 'mlops engineer'}
collection.delete_many(rec)
pymongo delete_many

Pymongo deleted record count

Sometimes you might need to know how many records get deleted for a particular filter condition.

MongoDB provides the deleted_count command to get the count of deleted documents.

rec= {'job': 'Data engineer'}
kl= collection.delete_many(rec)
kl.deleted_count
pymongo deleted count

Python MongoDB atlas

So far, we have deployed MongoDB in our local machine, and sometimes it is pretty painful to get all the things working.

To solve this issue, MongoDB provides a fully managed cloud-based MongoDB instance where you can effortlessly manage your MongoDB instance and scale it as per your need.

MongoDB Atlas is a fully managed cloud database developed by MongoDB. Atlas handles all the complexity of deploying, managing, and healing your deployments on the cloud service provider of your choice.

You can follow this link to deploy your first free mongo instance and play around.

Conclusion

Finally, we have come to the end of this pymongo tutorial. We started with the pymongo definition and went ahead and deployed pymongo in our local machine.

As we proceed, we have seen important CRUD operations with pymongo, and finally, we get an overview of the MongoDB Atlas service.

I hope you like this introductory pymongo tutorial. Please do let me know in the comment box if you find this tutorial useful.

More to Read

MongoDB commands

MongoDB create database

MongoDB create collection

MongoDB connection string

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top