---
name: mongodb
description: Interact with the MongoDB instance at docdb.connorrhodes.com. Use this skill whenever the user wants to query, insert, update, or delete data in MongoDB, list databases or collections, explore schema, run aggregations, or do anything involving their database. Also trigger when the user mentions "mongo", "docdb", or "database" in a context that could involve their self-hosted instance.
version: 1.0.0
---

# MongoDB Interaction

Connect to and interact with the MongoDB instance using Python (`pymongo`) with `uv` for dependency management.

## Connection Details

- **Host**: `docdb.connorrhodes.com`
- **Port**: `35563`
- **Username**: `root`
- **Password**: `3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl`
- **Connection string**: `mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563`

## How to Run Scripts

Always use `uv` with an inline dependency declaration so there's no project setup needed:

```bash
uv run --with pymongo script.py
```

This handles installing pymongo automatically. No venv or requirements.txt needed.

## Pattern for Scripts

Every script should follow this structure. The connection helper at `scripts/connect.py` can be imported to avoid repeating boilerplate — read it and copy it into a standalone script or import it directly:

```python
from pymongo import MongoClient

client = MongoClient("mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563")
```

### Exploring the database

When the user asks exploratory questions (e.g., "what's in my database?", "show me the data"), start by listing databases and collections to understand what's there before running targeted queries:

```python
from pymongo import MongoClient

client = MongoClient("mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563")

for db_name in client.list_database_names():
    print(f"Database: {db_name}")
    db = client[db_name]
    for coll_name in db.list_collection_names():
        count = db[coll_name].estimated_document_count()
        print(f"  {coll_name} ({count} docs)")
```

### Querying documents

```python
from pymongo import MongoClient

client = MongoClient("mongodb://root:3wwfoUjyk2E2zWELXFlLuHqfw1ALlOp4pb2H5Vq3TImbMIHL2h1u8Jej2mjzCPl@docdb.connorrhodes.com:35563")
db = client["database_name"]
collection = db["collection_name"]

for doc in collection.find({"field": "value"}).limit(10):
    print(doc)
```

### Inserting documents

```python
result = collection.insert_one({"key": "value"})
print(f"Inserted: {result.inserted_id}")
```

For bulk inserts:
```python
result = collection.insert_many([{"key": f"val{i}"} for i in range(100)])
print(f"Inserted {len(result.inserted_ids)} documents")
```

### Updating documents

```python
result = collection.update_one(
    {"field": "match_value"},
    {"$set": {"field": "new_value"}}
)
print(f"Matched: {result.matched_count}, Modified: {result.modified_count}")
```

### Deleting documents

```python
result = collection.delete_many({"field": "value"})
print(f"Deleted: {result.deleted_count}")
```

### Aggregation pipeline

```python
pipeline = [
    {"$group": {"_id": "$category", "count": {"$sum": 1}}},
    {"$sort": {"count": -1}}
]
for doc in collection.aggregate(pipeline):
    print(doc)
```

## Guidelines

- Always use `uv run --with pymongo` to execute scripts — no manual venv or pip.
- For one-off queries, write the script inline and run it directly. Don't create persistent files unless the user asks.
- When exploring an unfamiliar database, list databases and collections first with document counts, then sample a few documents from relevant collections before running the user's actual query.
- Use `.limit()` when previewing data to avoid dumping huge result sets.
- For large results, consider using `.count_documents(filter)` first to warn the user about scale.
- Pretty-print documents with `pprint` or JSON formatting for readability.
- Close the client when done: `client.close()` — though for short scripts this isn't strictly necessary.
