A python package for connecting with database.
Project description
PyMongo Automation
Overview
The PyMongo Automation is a Python package designed to facilitate MongoDB database operations using the PyMongo library. This package provides a set of utility functions to interact with MongoDB collections, including inserting, updating, deleting, and querying records, as well as exporting collections to various formats.
Features
- Insert records into MongoDB collections.
- Bulk insert data from CSV or Excel files.
- Export collections to Pandas DataFrames or save them as CSV/Excel files.
- Run aggregate pipelines and retrieve results as DataFrames.
- Backup and restore collections to/from JSON files.
- Count documents in a collection based on a query.
- Check if a collection exists and list all collections.
- Fetch a random sample of documents from a collection.
Installation
To install the package, use pip:
pip install pymongo-automation
Usage
1. Initialization
from pymongo_automation.mongo_crud import mongo_operation
# Initialize the MongoDB operation helper
mongo = mongo_operation(client_url='your_mongo_db_url', database_name='your_database_name')
2. Inserting Records
Insert a Single Record
record = {"name": "Product A", "price": 25.99}
mongo.insert_record(record, collection_name="products")
Insert Multiple Records
records = [
{"name": "Product B", "price": 19.99},
{"name": "Product C", "price": 29.99}
]
mongo.insert_record(records, collection_name="products")
3. Bulk Insert from CSV or Excel
Insert from a CSV File
mongo.bulk_insert(datafile="path/to/file.csv", collection_name="products")
Insert from an Excel File
mongo.bulk_insert(datafile="path/to/file.xlsx", collection_name="products")
4. Export Collection to DataFrame or File
Export Collection to a Pandas DataFrame
df = mongo.export_collection_to_df(collection_name="products", query={})
print(df)
Export Collection to a CSV File
mongo.export_collection_to_file(collection_name="products", file_path="products.csv", query={}, file_format='csv')
5. Update Records
query = {"name": "Product A"}
update_values = {"price": 30.99}
mongo.update_records(collection_name="products", query=query, update_values=update_values)
6. Delete Records
query = {"name": "Product A"}
mongo.delete_records(collection_name="products", query=query)
7. Count Documents in a Collection
query = {"price": {"$gt": 20}}
count = mongo.count_documents(collection_name="products", query=query)
print(f"Number of documents: {count}")
8. Run an Aggregate Pipeline
Example 1: Grouping and Counting by Name
pipeline = [
{'$group': {'_id': '$name', 'count': {'$sum': 1}}}
]
df = mongo.run_aggregate_pipeline('products', pipeline)
print(df)
Example 2: Summing Total Stock
pipeline = [
{'$group': {'_id': None, 'total_stock': {'$sum': '$stock'}}}
]
df = mongo.run_aggregate_pipeline('products', pipeline)
print(f"Total Stock: {df['total_stock'][0]}")
9. Backup and Restore Collection
Backup Collection to a JSON File
mongo.backup_collection(collection_name="products", backup_file="products_backup.json")
Restore Collection from a JSON File
mongo.restore_collection(collection_name="products_restored", backup_file="products_backup.json")
10. Check Collection Existence and List Collections
Check if a Collection Exists
exists = mongo.collection_exists(collection_name="products")
print(f"Collection exists: {exists}")
List All Collections
collections = mongo.list_collections()
print(f"Collections: {collections}")
11. Fetch a Random Sample of Documents
df_sample = mongo.fetch_random_sample(collection_name="products", sample_size=5)
print(df_sample)
Contributing
Contributions are welcome! If you have any suggestions, bug reports, or pull requests, please feel free to reach out or open an issue on the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymongo_automation-0.0.2.tar.gz.
File metadata
- Download URL: pymongo_automation-0.0.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52d92cc505ee47f83cd1ab4843b58e87e2e2e54a8008120d9025a26292946b26
|
|
| MD5 |
396203bda76957c4e65e23640dedb22a
|
|
| BLAKE2b-256 |
b5c42e17eb45f3e5b0f81bd5705edd17fb38ce41e326e2db4ee15c4afee24bfd
|
File details
Details for the file pymongo_automation-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pymongo_automation-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
888422d3c9b76a7a6139c69167b7c7702f019e6a008d77451aff3e4022bf5ba2
|
|
| MD5 |
60fab8c41619cddeb10a11d8489ebac6
|
|
| BLAKE2b-256 |
54cdaa4a5f26af21e8e845163abd7a3b90d9d35e7d9748dbc180c6c11167c02d
|