Thursday, January 15, 2015

Installing the Python Driver in MongoDB

Installing Python on Ubuntu 14.04:

  1. Check to see if pip is already installed
    whereis pip
    pip is a package management system used to install and manage software packages written in Python.
  2. Install python-dev
    sudo apt-get install build-essential python-dev
    header files and a static library for Python
  3. Install pip
    sudo apt-get install python-pip
  4. Install pymongo
    sudo pip install pymongo
    PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python


Simple Script


 import datetime  
 import pprint  
 import pymongo  

 from pymongo import MongoClient  

 client = MongoClient('mongodb://localhost:27017/')  
 db = client.mydb  

 post = { "a" : datetime.datetime.utcnow() }  
 postId = db.test.insert(post);  

 find = db.test.find()  
 for item in db.test.find() :   
      pprint.pprint(item)  

I've saved this script as "test.py" and it can be executed by typing
python test.py
in the terminal window.

Successful output for me looks like:
 craig@U14BASE01:~$ python test.py   
 {u'_id': ObjectId('54b8048557566138d66f7fa8'),  
  u'a': datetime.datetime(2015, 1, 15, 18, 18, 45, 239000)}  

1 comment:

  1. hi,this is excellent information..we provide by very easy learning good information.


    MongoDB Training Centers in Chenai

    ReplyDelete