{"id":27941,"date":"2021-07-17T15:15:57","date_gmt":"2021-07-17T15:15:57","guid":{"rendered":"https:\/\/naiveskill.com\/?p=27941"},"modified":"2022-11-01T09:03:34","modified_gmt":"2022-11-01T09:03:34","slug":"mongodb-with-python","status":"publish","type":"post","link":"https:\/\/naiveskill.com\/mongodb-with-python\/","title":{"rendered":"MongoDB with python complete tutorial | Learn pymongo in 2022"},"content":{"rendered":"\n

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<\/strong>. <\/p>\n\n\n\n

We can interact with MongoDB using the mongo<\/em> 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<\/a>.<\/p>\n\n\n\n

How to connect MongoDB with python<\/h2>\n\n\n\n

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.<\/p>\n\n\n\n

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

To connect MongoDB with python, we can use the pymongo driver. It is the official driver provided by MongoDB.<\/p>\n\n\n\n

What is Pymongo<\/h2>\n\n\n\n

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.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

Now it is time to get started with the pymongo installation<\/p>\n\n\n\n

Install pymongo<\/h2>\n\n\n\n

Installing pymongo is relatively straightforward. You can install pymongo with simple pip command.<\/p>\n\n\n\n

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

Once you have a working MongoDB instance, you can connect to MongoDB docker containers and type the below command to install pymongo.<\/p>\n\n\n\n

apt-get update\napt-get install python3\napt-get install python3-pip\npip install ipython<\/pre>\n\n\n\n
pip install pymongo\nCollecting pymongo\n  Downloading pymongo-3.12.0-cp38-cp38-manylinux2014_x86_64.whl (545 kB)\n     |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 545 kB 1.6 MB\/s\nInstalling collected packages: pymongo\nSuccessfully installed pymongo-3.12.0<\/pre>\n\n\n\n

<\/p>\n\n\n\n

Type ipython to connect to interactive python shell in MongoDB docker container and import pymongo package to see if the installation is working fine<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

If the pymongo packages get successfully imported without any error, it means your pymongo installation is OK.<\/p>\n\n\n\n

Connect mongodb with python<\/h2>\n\n\n\n

To connect to MongoDB using python, we need to import the pymongo package<\/p>\n\n\n\n

import pymongo<\/pre>\n\n\n\n

Now the next step is the create a MongoClient.The MongoClient takes the MongoDB connection string as a parameter.<\/p>\n\n\n\n

Since our MongoDB is running locally on port 27017, we use the below connection string to connect to MongoDB.<\/p>\n\n\n\n

client = pymongo.MongoClient(\"mongodb:\/\/localhost:27017\/\")<\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

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.<\/p>\n\n\n\n

Python mongodb connection with username and password<\/h3>\n\n\n\n

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. <\/p>\n\n\n\n

import pymongo\nclient = pymongo.MongoClient(\"mongodb:\/\/[username:password@]localhost:27017\/\")<\/pre>\n\n\n\n

<\/p>\n\n\n\n

Python MongoDB query<\/h2>\n\n\n\n
\"pymongo<\/figure>\n\n\n\n

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<\/a> before proceeding further.<\/p>\n\n\n\n

Pymongo create database and collections<\/h3>\n\n\n\n

This session will help you to create a database and collection in MongoDB.MongoDB collections are similar to tables in a relational database.<\/p>\n\n\n\n

Pymongo create a database<\/h4>\n\n\n\n

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<\/p>\n\n\n\n

db = client['naiveskill']<\/pre>\n\n\n\n

Pymongo create a collection<\/h4>\n\n\n\n

Once the DB gets created, type the below command to create a collection called myCollection<\/em>.<\/p>\n\n\n\n

collection = db['myCollection']<\/pre>\n\n\n\n

NOTE<\/strong>: Collections will not be visible in MongoDB until or unless a document is inserted.<\/p>\n\n\n\n

Pymongo Insert Command<\/h3>\n\n\n\n

With MongoDB insert command, we can insert documents into the MongoDB.We have the flexibility to insert a single document or multiple documents simultaneously<\/p>\n\n\n\n

Pymongo insert one<\/h4>\n\n\n\n

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.<\/p>\n\n\n\n

Type the below command to insert a single document.<\/p>\n\n\n\n

dict1= {'job': 'softare engineer' , 'Grade':7}\n\ncollection.insert_one(dict1)<\/pre>\n\n\n\n
\"pymongo<\/figure>\n\n\n\n

NOTE<\/strong>: <\/p>\n\n\n\n