Monguo

connection – Connection to MongoDB

class monguo.connection.Connection

Manager the connnections.

classmethod connect(db_name=None, connection_name=None, replica_set=False, *args, **kwargs)

Connect to MongoDB.

Parameters:
  • db_name(optional): The name of database. You can set it through switch_database().
  • connection_name(optional): It will use the default value DEFAULT_CONNECTION_NAME if not set.
  • replica_set(optional): If true it will use MotorReplicaSetClient instead of MotorClient to create a new connection.
classmethod disconnect(connection_name=None)

Disconnect the connection.

Parameters:
  • connection_name(optional): The connection name. If not set it will disconnect the current connection.
classmethod get_connection(connection_name=None, pymongo=False)

Get a connection, return None if the specified connection hasn’t been created.

Parameters:
  • connection_name(optional): The connection name. If not set it will return the current connection.
  • pymongo(optional): If true it will return an instance of MongoClient or MongoReplicaSetClient otherwise MotorClient or MotorReplicaSetClient.
classmethod get_connection_name_list()

Return the connections’ names.

classmethod get_database(connection_name=None, db_name=None, pymongo=False)

Get a database. If the specified connection_name hasn’t been created it will raise a ConnectionError.

Parameters:
  • connection_name(optional): It will use the current connection if not set.
  • db_name(optional): Return the current database if not set.
  • pymongo(optional): If true it will return an instance of MongoDatabase otherwise MotorDatabase.
classmethod get_default_connection_name()

Return the name of current connection.

classmethod get_default_database_name()

Return the name of current database.

classmethod switch_connection(connection_name)

Switch to the specified connection.

Parameters:
  • connection_name: The connection you switch to.
classmethod switch_database(db_name)

Switch to the specified database.

Parameters:
  • db_name: The database you switch to.
***
Fork me on GitHub