Created
June 24, 2014 04:25
-
-
Save titanjer/c137e32cfcf8e5e0b41f to your computer and use it in GitHub Desktop.
mongomock getCollection example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
globalconnection = None | |
def get_mongodb_connect_url(): | |
if IS_PRODUCTION: | |
return ('mongodb://fuck:yourmother' | |
'mongodb-1.fuck.com:27017,' | |
'mongodb-2.fuck.com:27017,' | |
'mongodb-3.fuck.com:27017,' | |
'mongodb-4.fuck.com:27017') | |
else: | |
return ('mongodb://love:yourmother@' | |
'db-love.fuck.com:27017') | |
def getConnection(): | |
global globalconnection | |
while globalconnection is None: | |
try: | |
if not IS_TESTING: | |
globalconnection = MongoClient(get_mongodb_connect_url(), | |
w=1, | |
max_pool_size=500) | |
else: | |
import mongomock | |
globalconnection = mongomock.MongoClient() | |
except Exception: | |
globalconnection = None | |
return globalconnection | |
def closeConnection(): | |
global globalconnection | |
if globalconnection: | |
try: | |
globalconnection.close() | |
except: | |
pass | |
globalconnection = None | |
def getCollection(dbname, collname): | |
conn = getConnection() | |
return conn[dbname][collname] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment