1. Install mongodb
  2. Install pymongo
  3. sudo easy_install pymongo
  4. Connect with MongoClient
  5. import pymongo
    client = pymongo.MongoClient('localhost', 27017)
  6. Get a Database
  7. db = client.test_db
    # OR 
    db = client['test-db']
  8. Get a Collection
  9. collection = db.test_collection
    # OR
    collection = db['test-collection']
  10. Using insert [db.collection.insert()]
  11. document = { 'col1' : 1,
                 'col2' : 'helloworld',
                 'col3' : ['val1', 'val2', ],
               }
    collection.insert(document)
  12. Using find_one [db.collection.findOne()]
  13. document = collection.find_one()
    print document
  14. Using find [db.collection.find()]
  15. documents = collection.find({ 'query_col' : 'equals this' }, 
                                sort=[( 'sort_col', pymongo.ASCENDING )])
  16. Using remove [db.collection.remove()]
    # Remove using a dictionary
    document = collection.find_one()
    collection.remove(document)
    # Remove by ObjectID (document['_id'])
    collection.remove('50906d7fa3c412bb040eb577')
  17. Using update [db.collection.update()/db.collection.upsert()]
  18. document = collection.find_one()
    collection.update(document, 
                      {'$set' : { 'add_this_col' : 'new col' } },
                      upsert = False,
                      multi = False,
                     )
0