26.04.2010
Installing MongoDB is pretty straight forward. Just download the right package from download page on mongo website, and then unarchive it somewhere. Create in that root directory a folder for data (mkdir -p data/db) and then fire up the server with: bin/mongod –dbpath data/db/.
The python package is called PyMongo, and it's the right tool to work with MongoDB. An easy way to install it is to use easy_install tool.
Check this page to find out more information.
Creating a db is very simple. First you get a connection to the running mongod and then simply point to the database name. This will be created lazily style (when you actually will insert something).
To get a connection for a different host or port, use Connection('different_host', 12345)
In [1]: from pymongo import Connection In [2]: conn = Connection() In [3]: db = conn.example_db In [4]: db Out[4]: Database(Connection('localhost', 27017), u'example_db')
The process is similar to the database creation one.
In [5]: collection = db.collection_example
Data in MongoDB is represented and stored using JSON-style documents. In PyMongo we use dictionaries to represent documents and to insert data the insert method is used.
In [6]: author_data = { "author" : "John Doe", "age": 54, "hobbies": [ "ski", "music", "writing poems" ]} In [7]: collection.insert(author_data) Out[7]: ObjectId('4bd5b83790dedd17d8000000')
When a document is inserted a special key, “_id”, is automatically added if the document doesn’t already contain an “_id” key. The value of “_id” must be unique across the collection. insert() returns the value of “_id” for the inserted document. For more information, see the documentation on _id.
To insert multiple documents, we pass an iterator to the insert() method like this:
In [16]: new_authors = [ { "author" : "Good Joe", "age": 22, "hobbies": [ "writing code", "music", "literature" ]},
{ "author" : "Ryan Zoe", "age": 44, "hobbies": [ "chess", "literature" ]} ]
In [19]: collection.insert(new_authors)
Out[19]: [ObjectId('4bd5b9c890dedd17d8000001'), ObjectId('4bd5b9c890dedd17d8000002')]
To get all the collections available in our database, use this:
In [8]: db.collection_names() Out[8]: [u'collection_example', u'system.indexes']
Methods: find_one and find.
In [23]: collection.find_one({"author": "John Doe"}) Out[23]: {u'_id': ObjectId('4bd5b83790dedd17d8000000'), u'age': 54, u'author': u'John Doe', u'hobbies': [u'ski', u'music', u'writing poems']}
In [32]: for author in collection.find(): ....: print "Author: %s Age: %s" % (author["author"], author["age"]) ....: Author: John Doe Age: 54 Author: Good Joe Age: 22 Author: Ryan Zoe Age: 44
Using Regex in search function (don't forget to run first import re if it's not there already):
In [94]: collection.find({ "author": re.compile("John.*") }).count() Out[94]: 1
collection.find({ "age" : { "$lt": 30 } }).count() # find all records with age less than 30
For advanced queries check http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions.