Create A MongoDB Database On Microsoft Azure

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.

Prerequisites
Installation

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.



Pymongo (Python driver for MongoDB) The PyMongo distribution contains tools for interacting with MongoDB database from Python. The bson package is an implementation of the BSON format for Python. The pymongo package is a native Python driver for MongoDB. The gridfs package is a gridfs implementation on top of pymongo.



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
  1. from pymongo import MongoClient  
  2. Connect to the database  
  3. client = MongoClient('<Connection String>')  
  4. db = client.pythonMongoDB  
  5. posts = db.posts  
  6. post = {'firstname':'Romil','lastname':'Bheda'}  
  7. id_pythonmongo = posts.insert(post)  
  8. print 'Create the id: %s'%post  
  9. client.close()  
Save it in C:\Python27 folder.

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,

  1. from pymongo import MongoClient  
  2. client = MongoClient('<ConnectionString>')  
  3. db = client.pythonMongoDB  
  4. results_posts = db.posts.find()  
  5. for post in results_posts:  
  6. print post  
  7. client.close()  
Now run the script.

C:\Python27>python read.py



Congratulations, you have successfully inserted & read data using MongoDB on Microsoft Azure!
 
Read more articles on Azure:


Similar Articles