How to connect to MongoDb from Python

Pass this to the next buddy!

MongoDB is among the top used NoSQL databases around the world. Well, i know you know that already. That is not,however the reason why i love mongoDB. Perhaps i should mention to you that MongoDB is Open Source. Yeah, that is why.

Conectiong to MongoDb can be done in almost any language. I have done works using MongoDB with Nodejs, PHP, Java among others. This therefore means, you should not worry about MongoDB drivers not being available in your preferred language.

Why MongoDB

  • Well, Mongodb is NoSQL meaning it is VERY fast & scalable – huh! everyone including you will want quick apps, websites etc.Quick is the keyword here.
  • It is free and Opensource unless you need hosting

Why Python + MongoDB

Well, am sure you do not really need an answer for this but here we go:-

  • First, it is the title of this article
  • Secondly, Python is king of Artificial Intelligent(AI). AI is King of Intensive Data manipulation and MongoDB is King of Speed and scalability

That is why.

Getting Started


Install PyMongo

PyMongo is the MongoDB Driver for Python. Use python’s package/module manager (pip) to install.

In python 3 or higher, pip is automatically installed and therefore you do not need to install it separately.

$ python -m pip install PyMongo

Importing PyMongo Module in your project

import pymongo

Opening MongoDB Connection

This is how you open the connection from python. Below your import command add the following.

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

Create your First database

To create a database named “riorityoverride“, add the following:

dbcreated = conn.riorityoverride

Create a collection

A collection in MongoDB is the equivalent of a Table in SQL. Let us create collection called twitter_profiles in the database riorityoverride

collectioncreated = dbcreated.twitter_profiles

NOTE: Up to this point, all is well and code executes perfectly. However, if you open MongoDB compass, you will realize that no database is created and no collection too. Relax. This is normal. MongoDB does not keep “empty things

Inserting Data into a collection

MongoDB uses json format for storage. To insert data into a collection utilize the python Dict data structure.

profiles_dict = [
{ "name": "Ayan", "Followers": 1100, "Following": 110},
{ "name": "Everest", "Followers": 357, "Following": 4},
{ "name": "user345", "Followers": 34000, "Following": 2300},
{ "name": "user346", "Followers": 456, "Following": 986},
{ "name": "user347", "Followers": 3200, "Following": 234},
{ "name": "user348", "Followers": 100456, "Following": 203872}
insertion_task = collectioncreated.insert_many(profiles_dict)

NOTE: In MongoDB it is only after inserting some data that everything else gets created. After the above code excutes, if you open MongoDB Compass, you will see our database, collection and the data in the collection.

Wrapping up

That is it for today’s class. If you like it share, if you dont like it comment below your feedback.

Authors: Ayan, J.O Iddris

Ayan:Java programmer(@riorityoverride), J.O.Iddris: A.I. programmer

Pass this to the next buddy!

Howto-daily Team

Howto-daily team is a group of volunteering writers who are working very hard to achieve the free-knowledge access goals. Ack:- J. Jay, I.O. Iddris, I.J.Kinley, I.I. Kim. Email letmehelp AT howto-daily dot com.

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.