A python package for connecting with database.
Project description
Overview
mdb_connect_pkg is a Python package designed to simplify the process of connecting to and interacting with MongoDB databases. This package provides a streamlined API for performing CRUD (Create, Read, Update, Delete) operations, handling connections, and managing MongoDB collections with ease.
Features
Easy connection management to MongoDB instances
Simple CRUD operations
Handling of MongoDB collections
Directory Structure
mongodb-connector-pkg/
├── .github/
│ └── workflows/
│ └── ci.yaml
│ └── python-publish.yaml
├── src/
│ └── mdb_connect_pkg/
│ └── mongo_crud.py
├── tests/
│ ├── unit/
│ │ └── test_unit.py
│ └── integration/
│ └── test_integration.py
├── .gitignore
├── init_setup.sh
├── LICENSE
├── pyproject.toml
├── README.md
├── README.rst
├── requirements_dev.txt
├── requirements.txt
├── setup.cfg
├── setup.py
├── template.py
└── tox.ini
How to Use This Package
Installation
To install the package, use pip:
pip install mdb-connect-pkg==0.0.5
Usage
Connecting to MongoDB
First, import the package and create a connection instance:
# Importing the package
from mdb_connect_pkg import mongo_crud
# Initialize the connector
db_connection_url = 'mongodb://localhost:27017'
database_name = 'mydatabase'
collection_name = 'mycollection'
mongo = mongo_crud.MongoDBConnection(db_connection_url, database_name, collection_name)
# Creating the client and connect to the database
mongo.create_mongo_client()
mongo.database_()
mongo.collection_()
CRUD Operations
Create
To insert a single record into the collection:
mongo.single_insert({"name": "John Doe", "age": 30, "email": "johndoe@example.com"})
To insert a JSON file into the collection:
json_file_path = 'example.json'
mongo.bulk_insert(json_file_path)
To insert a CSV file into the collection:
csv_file_path = 'example.csv'
mongo.bulk_insert(csv_file_path)
Read
To load the data from the MongoDB database to a pandas DataFrame:
df = mongo.load_data()
df.head()
Update
To update the records in the collection:
mongo.update_data({"name": "John Doe"},{"$set": {"age": 31}})
Delete
To delete records in the collection:
mongo.delete_record({"name": "John Doe"})
To delete the entire collection:
mongo.drop_collection()
To delete the entire database:
mongo.drop_database()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mdb_connect_pkg-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e867fabbd965fb0df6e384ed1119241d17e8a790ada243310a42a8c5b1538859 |
|
MD5 | 487817f9ba8011e749efc9ad7a9c3d65 |
|
BLAKE2b-256 | 86954ec554f9164d77ff1fc479017eca06d33cd25e13e6fbe2fcc16b9feaa20a |