Getting Started with Python™ and MongoDB®

3 min read
Getting Started with Python™ and MongoDB®


ScaleGrid is a MongoDB® hosting and management service for public and private clouds. MongoDB (from “humongous”) is a scalable, high-performance, open source NoSQL database by 10gen.

ScaleGrid helps you provision, configure high availability and disaster recovery, deprovision, monitor, upgrade, clone, backup and recover your deployments, supporting MongoDB® on AWS, MongoDB® on Azure, and MongoDB® on DigitalOcean. One of the advantages of ScaleGrid is that it gives you full SSH access to your instances, which enables you to run your Python server on the same machine as your MongoDB® server. This is extremely useful for dev and test scenarios.  In five easy steps, you can get up and running with your MongoDB and Python code.

Create your MongoDB® Instance on ScaleGrid

Follow these getting started directions to create your machine pool, create MongoDB® instances, retrieve SSH credentials and SSH into the instance, or learn how to create a MongoDB® cluster through our help documentation.

Connect to MongoDB® and Populate Your Data

SSH into your MongoDB® instance. Connect to your local MongoDB instance using the built-in mongo client and fire off some queries:

MongoDB shell version: 2.0.7
connecting to: test

> show dbs
config (empty)
local (empty)

> db.version()

> db.stats()
  "db" : "test",
  "collections" : 0,
  "objects" : 0,
  "avgObjSize" : 0,
  "dataSize" : 0,
  "storageSize" : 0,
  "numExtents" : 0,
  "indexes" : 0,
  "indexSize" : 0,
  "fileSize" : 0,
  "nsSizeMB" : 0,
  "ok" : 1

Let’s create a dummy database and insert some data into a collection. “Collections” are the equivalent of relational tables and can contain many “documents” which is the equivalent of rows in the relational world:

> use testdb
switched to db testdb

> db.testcollection.insert({"name":"blah", "value":"humbug"});

> db.testcollection.insert({"name":"blah1", "value":"humbug1"});

> db.find();
{ "_id" : ObjectId("50db292013d7f5d141a9cbfb"), "name" : "blah", "value" : "humbug" }
{ "_id" : ObjectId("50db292913d7f5d141a9cbfc"), "name" : "blah1", "value" : "humbug1" }

Setup Your Python Server

If you already have a Python server running on a machine, separate it from your MongoDB® server. You can then skip this step and move to step 4. If you don’t have a separate Python server, you can run Python on the MongoDB® machine. This is one of the benefits of having full SSH access to your MongoDB® machines. ScaleGrid machines have Python 2.6.8 installed by default.

Install PyMongo

MongoDB support in Python is through PyMongo. From the Python command line, enter the command below:

>>> import pymongo

Run Python Code

Retrieve the MongoDB® connection string from the ScaleGrid console in the details tab at the bottom of the screen.  If you’re running your Python code on the same box, you can use

Let’s write some Python code to query the documents in the collection we just created. Create a file called and put the code below into the file. Run the code using “python”.

from pymongo import Connection
connection = Connection()
connection = Connection('localhost', 27017)
db = connection.testdb
collection = db.testcollection
for post in collection.find():
        print post

For more detailed instructions and examples, refer to the 10gen documentation on using Python with Mongo®.

For more information, please visit Connect with ScaleGrid on LinkedIn, X, Facebook, and YouTube.
Table of Contents

Stay Ahead with ScaleGrid Insights

Dive into the world of database management with our monthly newsletter. Get expert tips, in-depth articles, and the latest news, directly to your inbox.

Related Posts

Redis vs Memcached in 2024

Choosing between Redis and Memcached hinges on specific application requirements. In this comparison of Redis vs Memcached, we strip away...

multi cloud plan - scalegrid

Plan Your Multi Cloud Strategy

Thinking about going multi-cloud? A well-planned multi cloud strategy can seriously upgrade your business’s tech game, making you more agile....

hybrid cloud strategy - scalegrid

Mastering Hybrid Cloud Strategy

Mastering Hybrid Cloud Strategy Are you looking to leverage the best private and public cloud worlds to propel your business...


Add Headline Here