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.

  1. 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.

  2. 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:

    
    /usr/bin/mongo
    MongoDB shell version: 2.0.7
    connecting to: test
    >show dbs
    config  (empty)
    local   (empty)
    >db.version()
    2.0.7
    >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" }
    
  3. 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.

  4. Install PyMongo

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

    >>> import pymongo
    
  5. 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 127.0.0.1.

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

    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.


Dharshan is the founder of ScaleGrid.io (formerly MongoDirector.com). He is an experienced MongoDB developer and administrator. He can be reached for further comment at @dharshanrg


  • krishna keshav

    Hey,
    I want to connect to mongodb using python script on other computer but in same network. How can we do that?

    • Suhas Jadhav

      You can use ssh client in python to connect to mongodb in another computer using its ip

0 Shares
+1
Tweet
Share
Share
Pin