다음을 통해 공유


Azure: Create a MongoDB database

In this tutorial, we'll create a simple application using Python & MongoDB on Azure. MongoLabs is a cool service on Azure that allows full managed Mongo database.

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 the list. Sandbox option is also available. Enter MongoDB Name & select Region.

Review of MongoDB windows will available & click "Purchase".

Now wait for few second, MongoDB is creating a database on Azure.

Navigate to Microsoft Azure Portal & click CONNECTION INFO in bottom of the portal

Copy Connection string & paste in below code

   

Run Visual Studio Code or Notepad and enter 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()

Save it to 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 MANAGE option 

MongoDB database panel will open in another tab

Click Collection or Posts to display data

Also from the 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()

Now run the script

*C:\Python27>python read.py
*

Congratulations, you have successfully inserted & read data using MongoDB on Microsoft Azure!