Kanan Rahimov
Feb 25 · 2 min read

In this video, I will show how you can use Python to get connected to MongoDB. Using 2 sample programs we will write some information to the database and then receive it and iterate over the data and print to the console output.

We will not install MongoDB locally, instead we will use Docker and run official Mongo image.

Then, using following command we start it

docker run -d -p 27017:27017 --name m1 mongo

Here, one of the important part is -p option:
-p 27017:27107 exposes port 27017 so we can connect to mongodb instance from our local machine.

Once we have MongoDB running, we can start with Python code. First, let’s install PyMongo package:

pip install pymongo

Make sure you run this command within virtualenv so you do not install unnecessary packages globally. Shortly, you can setup virtualenv using following commands:

cd your-project-directory
virtualenv venv -p python3
source ./venv/bin/activate

For more information on pip and venv you can check this video:

Once we have Python project setup. We can use following code snippets to connect to MongoDB database and insert some data:

import pymongo  # package for working with MongoDBclient = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["customersdb"]
customers = db["customers"]
customers_list = [
{ "name": "Amy", "address": "Apple st 652"},
{ "name": "Hannah", "address": "Mountain 21"},
{ "name": "Michael", "address": "Valley 345"},
{ "name": "Sandy", "address": "Ocean blvd 2"},
{ "name": "Betty", "address": "Green Grass 1"},
{ "name": "Richard", "address": "Sky st 331"},
{ "name": "Susan", "address": "One way 98"},
{ "name": "Vicky", "address": "Yellow Garden 2"},
{ "name": "Ben", "address": "Park Lane 38"},
{ "name": "William", "address": "Central st 954"},
{ "name": "Chuck", "address": "Main Road 989"},
{ "name": "Viola", "address": "Sideway 1633"}
x = customers.insert_many(customers_list)# print list of the _id values of the inserted documents:

This code will open a connection, then insert some dummy data into so called customersdb database and customers collection (tables in relation database).

To get data and print it we can use following code:

import pymongoclient = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["customersdb"]
customers = db["customers"]
for x in customers.find():

If you’ve any feedback, feel free to tell me what I can improve, or provide better examples.

Show your love by following, sending claps and sharing this article or video. It will help me a lot and motivates to continue doing what I am doing :)

YouTube: https://youtube.com/CoderVlogger



Senior Coder, Junior Vlogger

Kanan Rahimov

Written by

Software Engineer. Author @CoderVlogger. Check https://kenanbek.github.io/ for more posts and updates.


Senior Coder, Junior Vlogger

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade