home / skills / doanchienthangdev / omgkit / mongodb

This skill helps you design and query MongoDB schemas, migrations, and aggregations with Mongoose, optimizing data modeling and performance.

npx playbooks add skill doanchienthangdev/omgkit --skill mongodb

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.4 KB
---
name: Developing with MongoDB
description: The agent implements MongoDB NoSQL database solutions with document modeling, aggregation pipelines, and Mongoose ODM. Use when building document-based applications, designing schemas, writing aggregations, or implementing NoSQL patterns.
---

# Developing with MongoDB

## Quick Start

```typescript
// Schema with Mongoose
import mongoose, { Schema, Document } from 'mongoose';

interface IUser extends Document {
  email: string;
  profile: { firstName: string; lastName: string };
  createdAt: Date;
}

const userSchema = new Schema<IUser>({
  email: { type: String, required: true, unique: true, lowercase: true },
  profile: {
    firstName: { type: String, required: true },
    lastName: { type: String, required: true },
  },
}, { timestamps: true });

userSchema.index({ email: 1 });
export const User = mongoose.model<IUser>('User', userSchema);
```

## Features

| Feature | Description | Guide |
|---------|-------------|-------|
| Document Schema | Type-safe schema design with Mongoose | Embed related data, use references for large collections |
| CRUD Operations | Create, read, update, delete with type safety | Use `findById`, `findOne`, `updateOne`, `deleteOne` |
| Aggregation Pipelines | Complex data transformations and analytics | Chain `$match`, `$group`, `$lookup`, `$project` stages |
| Indexing | Query optimization with proper indexes | Create compound indexes matching query patterns |
| Transactions | Multi-document ACID operations | Use sessions for operations requiring atomicity |
| Change Streams | Real-time data change notifications | Watch collections for inserts, updates, deletes |

## Common Patterns

### Repository Pattern with Pagination

```typescript
async findPaginated(filter: FilterQuery<IUser>, page = 1, limit = 20) {
  const [data, total] = await Promise.all([
    User.find(filter).sort({ createdAt: -1 }).skip((page - 1) * limit).limit(limit),
    User.countDocuments(filter),
  ]);
  return { data, pagination: { page, limit, total, totalPages: Math.ceil(total / limit) } };
}
```

### Aggregation Pipeline

```typescript
const stats = await Order.aggregate([
  { $match: { status: 'completed', createdAt: { $gte: startDate } } },
  { $group: { _id: '$userId', total: { $sum: '$amount' }, count: { $sum: 1 } } },
  { $sort: { total: -1 } },
  { $limit: 10 },
]);
```

### Transaction for Order Creation

```typescript
const session = await mongoose.startSession();
try {
  session.startTransaction();
  await Product.updateOne({ _id: productId }, { $inc: { stock: -quantity } }, { session });
  const order = await Order.create([{ userId, items, total }], { session });
  await session.commitTransaction();
  return order[0];
} catch (error) {
  await session.abortTransaction();
  throw error;
} finally {
  session.endSession();
}
```

## Best Practices

| Do | Avoid |
|----|-------|
| Design schemas based on query patterns | Embedding large arrays in documents |
| Create indexes for frequently queried fields | Using `$where` or mapReduce in production |
| Use `lean()` for read-only queries | Skipping validation on writes |
| Implement pagination for large datasets | Storing large files directly (use GridFS) |
| Set connection pool size appropriately | Hardcoding connection strings |
| Use transactions for multi-document ops | Ignoring index usage in explain plans |
| Add TTL indexes for expiring data | Creating too many indexes (write overhead) |

Overview

This skill implements MongoDB NoSQL solutions for Node.js applications using Mongoose and native MongoDB patterns. It focuses on document modeling, aggregation pipelines, indexing, transactions, and real-time change streams. Use it to build reliable, scalable document-based APIs and analytics flows.

How this skill works

The skill provides type-safe Mongoose schema examples and model patterns, repository-style query helpers, and aggregation pipeline templates. It shows how to create indexes, run multi-document transactions with sessions, and use change streams for realtime notifications. It also includes pagination, optimization tips, and common CRUD idioms for production apps.

When to use it

  • Building REST or GraphQL APIs backed by MongoDB
  • Designing document schemas and relations with Mongoose
  • Implementing complex aggregations or analytics pipelines
  • Ensuring multi-document atomicity with transactions
  • Optimizing queries with indexes and explain plans

Best practices

  • Design schemas around query patterns, not just data shape
  • Create compound indexes that match common queries and sort orders
  • Use lean() for high-volume read-only queries to reduce memory overhead
  • Implement pagination and limit result sets to avoid large responses
  • Use sessions for multi-document transactions and commit/abort safely

Example use cases

  • User profile and account modeling with email uniqueness and timestamps
  • Top-N analytics using aggregation pipelines for sales or engagement
  • Repository pattern with paginated user lists and total counts
  • Order creation workflow that decrements stock and creates an order inside a transaction
  • Realtime notifications by watching collections with change streams

FAQ

When should I use embedding vs referencing?

Embed small, frequently-read related data to reduce joins. Use references for large or growing relations and when data is shared across many documents.

How do I optimize slow queries?

Analyze queries with explain(), add indexes that match filter+sort patterns, avoid scanning large arrays, and use projection or lean() to reduce payload size.