Skip to content

Instantly share code, notes, and snippets.

@BekoBou
Created November 23, 2016 13:57
Show Gist options
  • Save BekoBou/5f7aa09cdbff19284b60a7ada957a9c7 to your computer and use it in GitHub Desktop.
Save BekoBou/5f7aa09cdbff19284b60a7ada957a9c7 to your computer and use it in GitHub Desktop.
MongoDB connection helper
# 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