from pymongo import MongoClient import pprint client = MongoClient('mongodb://localhost:27017/') tesla_s = { "manufacturer" : "Tesla Motors", "class" : "full-size", "body style" : "5-door liftback", "production" : [2012, 2013], "model years" : [2013], "layout" : ["Rear-motor", "rear-wheel drive"], "designer" : { "firstname" : "Franz", "surname" : "von Holzhusen" } } db = client.examples db.autos.insert(tesla_s) for a in db.autos.find(): pprint.pprint(a)
query
from pymongo import MongoClient import pprint client = MongoClient('mongodb://localhost:27017/') db = client.examples def find(): autos = db.autos.find({ "manufacturer" : "Toyota" }) for a in autos: pprint.pprint(a) if __name__ == '__main__': find()
#!/usr/bin/env python def porsche_query(): query = {} return query def get_db(db_name): from pymongo import MongoClient client = MongoClient('localhost:27017') db = client[db_name] return db def find_porsche(db, query): return db.autos.find(query) if __name__ == "__main__": db = get_db('examples') query = porsche_query() results = find_porsche(db, query) print "Printing first 3 result\n" import pprint for car in rsults[:3]: pprint.pprint(car)