User Tools

Site Tools


db:mongo_pythondriver

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

db:mongo_pythondriver [2013/03/16 17:40] (current)
Line 1: Line 1:
 +==== MongoDB and Python driver ====
 +26.04.2010
  
 +
 +=== Installing MongoDB ===
 +
 +Installing MongoDB is pretty straight forward. Just download the right package from [[http://​www.mongodb.org/​display/​DOCS/​Downloads | 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///.
 +
 +
 +=== Installing PyMongo ===
 +
 +The python package is called [[http://​api.mongodb.org/​python/​1.6/​index.html | PyMongo]], and it's the right tool to work with MongoDB. An easy way to install it is to use //​easy_install//​ tool. \\ 
 +Check [[http://​api.mongodb.org/​python/​1.6/​installation.html | this page]] to find out more information.
 +
 +
 +=== Creating/​Connecting to a DB ===
 +
 +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)//
 +
 +
 +<code python>
 +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'​)
 +</​code>​
 +
 +
 +=== Collections ===
 +
 +The process is similar to the database creation one.
 +
 +<code python>
 +In [5]: collection = db.collection_example
 +</​code>​
 +
 +=== Entering Data (Inserting documents) ===
 +
 +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.
 +
 +<code python>
 +In [6]: author_data = { "​author"​ : "John Doe", "​age":​ 54, "​hobbies":​ [ "​ski",​ "​music",​ "​writing poems" ]}
 +
 +In [7]: collection.insert(author_data)
 +Out[7]: ObjectId('​4bd5b83790dedd17d8000000'​)
 +</​code>​
 +
 +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:
 +
 +<code pyhton>
 +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'​)] ​               ​
 +</​code>​
 +
 +
 +To get all the collections available in our database, use this:
 +
 +<code python>
 +In [8]: db.collection_names()
 +Out[8]: [u'​collection_example',​ u'​system.indexes'​]
 +</​code>​
 +
 +
 +=== Querying the collections ===
 +
 +Methods: //​find_one//​ and //find.//
 +
 +<code python>
 +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'​]}
 +</​code>​
 +
 +
 +<code python>
 +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
 +</​code>​
 +
 +
 +Using Regex in search function (don't forget to run first import re if it's not there already):
 +
 +<code python>
 +In [94]: collection.find({ "​author":​ re.compile("​John.*"​) }).count()
 +Out[94]: 1
 +</​code>​
 +
 +<code python>
 +collection.find({ "​age"​ : { "​$lt":​ 30 } }).count() # find all records with age less than 30
 +</​code>​
 +
 +
 +For advanced queries check http://​www.mongodb.org/​display/​DOCS/​Advanced+Queries#​AdvancedQueries-RegularExpressions.
db/mongo_pythondriver.txt · Last modified: 2013/03/16 17:40 (external edit)