Using Python with MongoDB on Raspberry Pi 2 & 3

mongodb plus pythonI’ve written about getting MongoDB running on the Raspberry Pi 2. View my other posts where you can get binaries (3.0.9) or learn how to compile from scratch (3.0.7). The mongo shell works great but you may want/need to code in Python, especially for┬ádevice control or data logging, etc.

PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python. You can either use Python 2 or Python 3. Python 3 did not come on the minimal Raspbian Jessie image but can be installed using “sudo apt-get install python3”.

To install the appropriate PyMongo for MongoDB 3.0.x you can do the following from the command line. Note you could use “python3” where I use “python”, depending on your preference. I normally use the default Python 2.7.

Instructions after the break:

Ensure you do not have an old pymongo or python-pip:

sudo apt-get remove python-pip
sudo apt-get remove pymongo

Install “pip”, a python installer tool:

sudo python

Install PyMongo 3.0.3:

sudo python -m pip install pymongo==3.0.3

Now check it out!

$ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import MongoClient
>>> client = MongoClient('mongodb://localhost:27017/')
>>> db = client['local']
>>> collection = db['local']
>>> startlog = db.startup_log
>>> startlog.find_one()

PyMongo 3.0.3 documentation and a tutorial can be found at

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top