In this tutorial, we'll create a simple application using Python & MongoDB on Azure. MongoLabs is a cool service on Azure thats allows a fully managed Mongo database. Using this we will perform insert & read operation in MongoDB using Python.
- Microsoft Azure subscription (MSDN Subscription or Sign up for one month trial)
- Python 2.7.11 or Python 3.5.1
- Pymongo (Python driver for MongoDB)
- Visual Studio Code or Notepad
Should I use Python 2 or Python 3 for my development activity?
Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is under active development and has already seen over five years of stable releases, including version 3.3 in 2012, 3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only available by default in Python 3.x.Visual Studio Code
Create a MongoDB
Navigate to https://manage.windowsazure.com and sign in using Azure subscription.
Once you have logged into Microsoft Azure classic portal, you will find a variety of options available on the left side.
Select +NEW -> MARKETPLACE
After selecting MARKETPLACE option, all developer service available & select "mLab MongoDB".
Now click on Next.
Choose Application and service plan from list. Sandbox option also available. Enter MongoDB Name & select Region.
Review of MongoDB windows will available & click on "Purchase".
Now wait for few seconds, MongoDB is creating database on Azure.
Navigate to Microsoft Azure Portal & click on CONNECTION INFO in bottom of the portal
Copy Connection string & paste in below code
Run Visual Studio Code or Notepad& Enter below code:
create.py
- from pymongo import MongoClient
- # Connect to the database
- client = MongoClient('<Connection String>')
- db = client.pythonMongoDB
- posts = db.posts
- post = {'firstname':'Romil','lastname':'Bheda'}
- id_pythonmongo = posts.insert(post)
- print 'Create the id: %s'%post
- client.close()
Now Open command prompt (WIN + R then type cmd)
Change the directory with the help of below command:
type the below code & run the script
C:\Python27>python create.py
Data successfully stored on MongoDB
To check data on MongoDB, Click on MANAGE option
MongoDB database panel will open in another tab
Click on collection or posts to display data
Also from script we can get all data. create another script using Visual Studio Code or Notepad,
- from pymongo import MongoClient
- client = MongoClient('<ConnectionString>')
- db = client.pythonMongoDB
- results_posts = db.posts.find()
- for post in results_posts:
- print post
- client.close()
C:\Python27>python read.py
Congratulations, you have successfully inserted & read data using MongoDB on Microsoft Azure!








Suketu NayakPosted Jul 11, 2016, 8:18 AM
Excellent Work...Superb Article..Nice
Prasanna MuraliPosted May 10, 2016, 10:41 AM
nice
Vignesh ManiPosted Apr 17, 2016, 3:20 AM
Nice