Python 3 with MongoDB

A quick tutorial to demonstrate Python 3 with MongoDB

 Installing Python 3 and PyMongo

Download and install the latest version of Python 3 (version 3.3 at time of writing) from http://www.python.org/download/releases/

Make sure to select ‘Add python.exe to path’

Next we need to download the installer for Distribute for Python from http://python-distribute.org/distribute_setup.py . Now open an command prompt by opening the start menu
and typing cmd then press enter. Change to the folder where you downloaded Distribute (eg. cd c:\users\username\downloads) and run

python distribute_setup.py

This will download and install distribute to your Python 3 folder.

Now keep the command prompt open and change to the scripts folder inside the Python 3 folder (eg. cd c:\python33\scripts\) and run

easy_install pymongo

Once this completes, PyMongo is installed. We can test this by opening python in a command prompt and typing

import pymongo

If no errors occur then everything went ok and we can start using pymongo to work with MongoDB

Using PyMongo

We need to make a connection to work with a MongoDB database

from pymongo import Connection
connection=Connection() 
or 
connection=Connection('ip','port') if using a remote host

Next we need to select the database to use.

database=connection['mydatabase']

We don’t have to create databases or collections, if they don’t exists they will be created.

mycollection=database.entries

‘entries’ is a collection (similar to a table in a RDBMS) and will be created if needed.

Inserting Data in MongoDB

We store data in MongoDB using documents which are defined in Key Value pairs in PyMongo

post1={"title":"My first post","details":"This is my first entries on PyMongo"}

Then we insert the document into a collection

mycollection.insert(post1)

Now we’ll make a second document with a different set of columns.

post2={"title":"My second post","date":"05/11/2012","author":"Paul"}
mycollection.insert(post2)

We don’t have to use any of the same data names but it’s better to keep the documents some what similar in the same collection.

Retrieving one document

We use find({key:value}) to find a single document or we only want to get back the first document matching the condition.

print(mycollection.find_one({"title":"My second post"}))

This should return (ObjectId is unique to each document and assigned automatically)

{'title': 'My second post', 'date': '05/11/2012', '_id': ObjectId('509837c62fc1c02c91056697'), 'author': 'Paul'}

 Retrieving all documents

 for entry in mycollection.find():
    print(entry)

We can also filter the results like find_one()

for entry in mycollection.find("title":"My first post"):
    print(entry)

In the next tutorial, I’ll show how to update documents, delete documents and indexes

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close