dbComm
dbComm is a wrapper for pymongo that simplifies MongoDB database interactions with support for SSH tunneling and LDAP authentication.
Installation
pip install dbComm
Quick Start
from dbComm import Mongo
# Direct connection
db = Mongo(host="localhost", port=27017)
# SSH tunnel connection
db = Mongo(
host="remote.server",
port=27017,
ssh=True,
ssh_host="gateway.server",
database="mydb"
)
# LDAP authentication
db = Mongo(
host="mongodb.server",
authentication="LDAP"
)
Authentication Methods
Direct Connection
Basic MongoDB connection without authentication:
db = Mongo(host="localhost")
SSH Tunnel
Connect through an SSH tunnel:
db = Mongo(
host="internal.server",
ssh=True,
ssh_host="gateway.server",
ssh_username="user",
database="mydb"
)
LDAP Authentication
Connect using LDAP credentials:
db = Mongo(
host="mongodb.server",
authentication="LDAP",
username="ldap_user"
)
API Reference
Mongo Class
Constructor Parameters
host(str): MongoDB server hostnameport(int, optional): MongoDB port number. Default: 27017authentication(str, optional): Authentication type ('LDAP'). Default: Noneusername(str, optional): Username for authenticationpassword(str, optional): Password for authenticationssh(bool, optional): Use SSH tunnel. Default: Falsessh_host(str, optional): SSH gateway serverdatabase(str, optional): Database namessh_username(str, optional): SSH usernamessh_password(str, optional): SSH passwordtimeout(int, optional): Connection timeout in ms. Default: 5000
Methods
Database Operations
-
getDBs() -> List[str]Returns list of available databases -
setDB(db_name: str)Connect to specific database
Record Operations
-
getRecord(collection: str, field: Union[str, ObjectId, Dict])Retrieve single record from collection -
getRecords(collection: str) -> ListRetrieve all records from collection -
newRecord(collection: str, **kwargs) -> ObjectIdCreate new record in collection -
updateRecord(collection: str, field: Union[str, ObjectId, Dict], updateVals: Dict, updateType: str)Update existing record -
deleteRecords(collection: str)Delete all records in collection -
dropCollection(collection: str)Delete entire collection
File Operations
-
putFile(filepath: str, **kwargs) -> ObjectIdStore file in GridFS -
getFile(fileID: Union[str, ObjectId]) -> bytesRetrieve file from GridFS -
deleteFile(fileID: Union[str, ObjectId])Delete file from GridFS
Error Handling
The package raises standard MongoDB exceptions plus:
ValueError: Invalid database or collection namesTypeError: Invalid field typesException: Connection timeouts
Contributing
- Fork the repository
- Create feature branch
- Commit changes
- Push to branch
- Create Pull Request
License
see LICENSE