MongoDB DBA Skill
Prerequisites
This skill requires mongosh (MongoDB Shell) to be installed and available on PATH.
Connection Details
Connection settings are stored in the .NET API configuration:
- Check
appsettings.jsonatsrc/expenses-api/Expenses.Api/src/appsettings.jsonfor theMongoSettingssection:MongoSettings:ConnectionString— the MongoDB connection URIMongoSettings:Database— the database name
- User secrets may override these values. Check with:
cd src/expenses-api/Expenses.Api/src && dotnet user-secrets list - If neither source has credentials, ask the user for the connection string.
Connecting
mongosh "<ConnectionString>" --eval '<query>'
# Or for interactive exploration:
mongosh "<ConnectionString>"
Always target the correct database:
mongosh "<ConnectionString>" --eval 'use("<Database>"); <query>'
Database Schema
Refer to references/SCHEMA.md for detailed collection schemas. Summary:
| Collection | Description |
|---|---|
Expenses |
Individual expense records |
Categories |
Expense category definitions |
Tags |
Single document holding all tag names |
All field names are camelCase in MongoDB (via CamelCaseElementNameConvention). The _id fields are stored as ObjectId.
Common Queries
List recent expenses
db.Expenses.find().sort({ timestamp: -1 }).limit(10)
Expenses for a specific month
db.Expenses.find({
timestamp: {
$gte: ISODate("2026-01-01"),
$lt: ISODate("2026-02-01")
}
}).sort({ timestamp: -1 })
Aggregate total by category
db.Expenses.aggregate([
{ $group: { _id: "$category", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } }
])
Monthly spending totals
db.Expenses.aggregate([
{ $group: {
_id: { year: { $year: "$timestamp" }, month: { $month: "$timestamp" } },
total: { $sum: "$amount" },
count: { $sum: 1 }
}},
{ $sort: { "_id.year": -1, "_id.month": -1 } }
])
Search expenses by tag
db.Expenses.find({ tags: "groceries" })
List all categories
db.Categories.find()
List all tags
db.Tags.find()
Count expenses per category
db.Expenses.aggregate([
{ $group: { _id: "$category", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
Find top expenses by amount
db.Expenses.find().sort({ amount: -1 }).limit(10)
Safety Rules
- Read-only by default — Only execute read operations (
find,aggregate,count,distinct) unless the user explicitly requests a mutation. - Mutations require confirmation — Before running any
insert,update,delete,drop, orreplaceOneoperation, show the user the exact command and ask for explicit confirmation. - Never drop collections or databases without the user typing the exact confirmation.
- Always limit result sets — Use
.limit()on queries that could return large datasets to avoid overwhelming output. - Show the query before executing — Let the user review the
mongoshcommand before running it.