示例#1
0
文件: Base.py 项目: no2key/stock-labs
 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
示例#2
0
文件: Base.py 项目: no2key/stock-labs
 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
示例#3
0
文件: run.py 项目: Quanse/lvs-manager
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 !')
示例#4
0
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 !')
示例#5
0
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()
示例#6
0
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()