Created
November 23, 2016 13:57
-
-
Save BekoBou/5f7aa09cdbff19284b60a7ada957a9c7 to your computer and use it in GitHub Desktop.
MongoDB connection helper
This file contains 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
# from mongohelp import Connection | |
# db = Connection().database_name | |
# print repr(db.collection.find_one()) | |
# db = Connection('example.com:37017').database_name | |
# print repr(db.collection.find_one()) | |
# db = Connection( | |
# ('s1.example.com, s2.example.com'), | |
# 'replica_name' | |
# ).database_name | |
# print repr(db.collection.find_one()) | |
from pymongo import MongoClient, MongoReplicaSetClient | |
def Connection(hosts_or_uri='localhost:27017', replicaSet=None, **kwargs): | |
db = None | |
# pymongo.mongo_client.MongoClient([ | |
# host='localhost', | |
# port=27017, | |
# max_pool_size=10, | |
# document_class=dict, | |
# tz_aware=False, **kwargs | |
# ) | |
# pymongo.mongo_replica_set_client.MongoReplicaSetClient( | |
# hosts_or_uri, | |
# max_pool_size=10, | |
# document_class=dict, | |
# tz_aware=False, | |
# **kwargs | |
# ) | |
if replicaSet: | |
db = MongoReplicaSetClient( | |
hosts_or_uri, replicaSet=replicaSet, **kwargs) | |
else: | |
db = MongoClient(hosts_or_uri, **kwargs) | |
return db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment