Monday, March 12, 2012

MongoDB and Python

For my project I had to store and operate on a large amount of data. I was mostly using dictionaries and lists in Python. Since the storing entire dictionary/list in memory was impossible, I was looking for alternatives. The obvious fall back option was to use disk storage. Persistent storage databases like MongoDB allow directly storing python objects on disk.

To install MongoDB on windows, I followed the quick tutorial provided on the MongoDB site: http://www.mongodb.org/display/DOCS/Quickstart+Windows. Don't forget to check the --dbpath directive while running mongod.exe, as it helps to specify the database path.

Being new to MongoDB, and having prior experience with SQL, it is good to know how the two approaches differ. The tutorial provided at http://www.mongodb.org/display/DOCS/Tutorial, gave a very good overview of how MongoDB works.

To setup Python to interact with MongoDB, follow the instructions here: http://www.mongodb.org/display/DOCS/Python+Language+Center

To learn about setting some fields unique (apart from the _id field), and to create indexes to boost the search latency, read tutorials here http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue.

The book titled "MongoDB and Python:Patterns and Processes for the Popular Document-Oriented Database" provided all the needed python commands to interact with the database. It was very helpful.