def find(self, symbol): options = self.application.options connection = MongoClient(options.db_host, options.db_port, max_pool_size = 0, auto_start_request = True, use_greenlets = False) db = connection[options.db_name] response = db.symbols.find({'S': symbol}) connection.end_request() return response
def distinct(self, args): options = self.application.options connection = MongoClient(options.db_host, options.db_port, max_pool_size = 0, auto_start_request = True, use_greenlets = False) db = connection[options.db_name] response = db.command({"distinct": "symbols", "key": "S"}) connection.end_request() return response
class MongoSession(): def __init__(self): self.conn = MongoClient(settings['mongodb'],settings['mongodb_port']) #self.conn = MongoClient(settings['mongodb'],settings['mongodb_port'],auto_start_request=True) self.db = self.conn[settings['db']] def close(self): try: self.conn.end_request() self.conn.close() logging.info('close mongo connection Success !') except Exception, e: logging.warning('close mongo connection fail !')
class MongoSession(): def __init__(self): self.conn = MongoClient(settings['mongodb'], settings['mongodb_port']) #self.conn = MongoClient(settings['mongodb'],settings['mongodb_port'],auto_start_request=True) self.db = self.conn[settings['db']] def close(self): try: self.conn.end_request() self.conn.close() logging.info('close mongo connection Success !') except Exception, e: logging.warning('close mongo connection fail !')
def mongo(collection_name): """Context manager to handle mongo connectivity. Attributes: collection_name -- name of the mongo collection """ # may want to pass that in db_name too, later on, when multiple databases are needed if (not DB_NAME) or (not DB_URL): raise DatabaseError('Database information was not found in the environment variables.') else: mongo_client = MongoClient(DB_URL+DB_NAME) mongo_request = mongo_client.start_request() mongo_collection = mongo_client[DB_NAME][collection_name] yield mongo_collection mongo_client.end_request()
from pymongo import MongoClient client = MongoClient('mongodb://127.0.0.1:27017/') #create database db = client.test_database #create table/collection posts = db.posts #create document/row post = {"author": "Shippable", "text": "Demo"} #inserting the row into table/database post_id = posts.insert(post) #retrieve the values from database with client.start_request(): cursor = posts.find() for result in cursor: print result #deleting the data from table posts.remove() #dropping the database client.drop_database('test_database') #end the connection client.end_request()