Python MongoDB Tutorial using Docker

Kanan Rahimov
CoderVlogger
Published in
2 min readFeb 25, 2019

--

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:

--

--