pymongo-automation 0.0.2
A python package for connecting with database.
License: MIT
Requires Python: >=3.7

# 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:

```bash
pip install pymongo-automation
```

## Usage

### 1. Initialization

```python
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

```python
record = {"name": "Product A", "price": 25.99}
mongo.insert_record(record, collection_name="products")
```

#### Insert Multiple Records

```python
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

```python
mongo.bulk_insert(datafile="path/to/file.csv", collection_name="products")
```

#### Insert from an Excel File

```python
mongo.bulk_insert(datafile="path/to/file.xlsx", collection_name="products")
```

### 4. Export Collection to DataFrame or File

#### Export Collection to a Pandas DataFrame

```python
df = mongo.export_collection_to_df(collection_name="products", query={})
print(df)
```

#### Export Collection to a CSV File

```python
mongo.export_collection_to_file(collection_name="products", file_path="products.csv", query={}, file_format='csv')
```

### 5. Update Records

```python
query = {"name": "Product A"}
update_values = {"price": 30.99}
mongo.update_records(collection_name="products", query=query, update_values=update_values)
```

### 6. Delete Records

```python
query = {"name": "Product A"}
mongo.delete_records(collection_name="products", query=query)
```

### 7. Count Documents in a Collection

```python
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

```python
pipeline = [
    {'$group': {'_id': '$name', 'count': {'$sum': 1}}}
]

df = mongo.run_aggregate_pipeline('products', pipeline)
print(df)
```

#### Example 2: Summing Total Stock

```python
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

```python
mongo.backup_collection(collection_name="products", backup_file="products_backup.json")
```

#### Restore Collection from a JSON File

```python
mongo.restore_collection(collection_name="products_restored", backup_file="products_backup.json")
```

### 10. Check Collection Existence and List Collections

#### Check if a Collection Exists

```python
exists = mongo.collection_exists(collection_name="products")
print(f"Collection exists: {exists}")
```

#### List All Collections

```python
collections = mongo.list_collections()
print(f"Collections: {collections}")
```

### 11. Fetch a Random Sample of Documents

```python
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.