Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsBlogPro
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges
  • Chrome Extension
  • Skill Manager

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Mongodb Patterns

ASecurity

Document modeling, aggregation pipeline, indexing strategy, change streams, and multi-document transactions.

530 stars
0 votes
0 copies
1 views
Added 5/29/2026
ai-agentstypescriptgoreactexpressdatabase

Works with

cli

Security Analysis

A100/100

Scanned 5/29/2026

$npx -y skills add vibeeval/vibecosystem --skill mongodb-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Mongodb Patterns?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Mongodb Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/vibeeval-mongodb-patterns/badge)](https://www.skillsdirectory.com/skills/vibeeval-mongodb-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
Files
skill.md
---
name: mongodb-patterns
description: Document modeling, aggregation pipeline, indexing strategy, change streams, and multi-document transactions.
---

# MongoDB Patterns

Document database design and query optimization for MongoDB.

## Document Modeling Strategies

```typescript
// EMBED when: 1:1 or 1:few, data read together, child has no independent lifecycle
interface Order {
  _id: ObjectId
  customerId: ObjectId
  status: 'pending' | 'paid' | 'shipped'
  items: OrderItem[]        // Embedded - always read with order
  shippingAddress: Address  // Embedded - 1:1
  createdAt: Date
}

interface OrderItem {
  productId: ObjectId
  name: string              // Denormalized - avoid join at read time
  price: number             // Snapshot at purchase time
  quantity: number
}

// REFERENCE when: 1:many (unbounded), independent queries, shared across documents
interface Product {
  _id: ObjectId
  name: string
  price: number
  categoryId: ObjectId     // Reference - category queried independently
  reviews: never           // DON'T embed - unbounded array
}

// Bucket pattern: group time-series data into fixed-size documents
interface SensorBucket {
  _id: ObjectId
  sensorId: string
  startTime: Date
  endTime: Date
  count: number            // Track bucket fullness
  measurements: {          // Embed up to 200 per bucket
    timestamp: Date
    value: number
  }[]
}
```

## Indexing Strategy

```typescript
// Compound index: field order matters (ESR rule)
// Equality → Sort → Range
db.orders.createIndex({
  status: 1,       // Equality: exact match filter
  createdAt: -1,   // Sort: avoid in-memory sort
  total: 1         // Range: price > 100
})

// Partial index: only index documents matching filter (smaller index)
db.orders.createIndex(
  { customerId: 1, createdAt: -1 },
  { partialFilterExpression: { status: 'pending' } }
)

// Text index for search
db.products.createIndex({ name: 'text', description: 'text' })

// TTL index for auto-expiration
db.sessions.createIndex(
  { createdAt: 1 },
  { expireAfterSeconds: 86400 }  // Auto-delete after 24h
)

// Wildcard index for dynamic schemas
db.events.createIndex({ 'metadata.$**': 1 })
```

## Aggregation Pipeline

```typescript
// Sales analytics: top products by revenue per category
const pipeline = [
  // Stage 1: Filter date range
  { $match: {
    createdAt: { $gte: new Date('2025-01-01'), $lt: new Date('2025-02-01') },
    status: 'paid'
  }},

  // Stage 2: Unwind embedded items array
  { $unwind: '$items' },

  // Stage 3: Group by product
  { $group: {
    _id: '$items.productId',
    productName: { $first: '$items.name' },
    totalRevenue: { $sum: { $multiply: ['$items.price', '$items.quantity'] } },
    totalSold: { $sum: '$items.quantity' },
    orderCount: { $addToSet: '$_id' }
  }},

  // Stage 4: Add computed fields
  { $addFields: {
    orderCount: { $size: '$orderCount' },
    avgOrderValue: { $divide: ['$totalRevenue', { $size: '$orderCount' }] }
  }},

  // Stage 5: Sort by revenue descending
  { $sort: { totalRevenue: -1 } },

  // Stage 6: Limit to top 20
  { $limit: 20 },

  // Stage 7: Lookup category details
  { $lookup: {
    from: 'products',
    localField: '_id',
    foreignField: '_id',
    pipeline: [{ $project: { categoryId: 1 } }],
    as: 'product'
  }}
]

const results = await db.orders.aggregate(pipeline).toArray()
```

## Change Streams (Real-time Reactivity)

```typescript
async function watchOrderChanges(): Promise<void> {
  const pipeline = [
    { $match: {
      operationType: { $in: ['insert', 'update'] },
      'fullDocument.status': 'paid'
    }}
  ]

  // resumeAfter enables resuming from last processed change (crash recovery)
  const changeStream = db.orders.watch(pipeline, {
    fullDocument: 'updateLookup',  // Include full document on updates
    resumeAfter: await getLastResumeToken()
  })

  changeStream.on('change', async (event) => {
    try {
      await processOrderPayment(event.fullDocument!)
      await saveResumeToken(event._id)  // Persist for crash recovery
    } catch (err) {
      console.error('Change stream processing failed:', err)
    }
  })

  changeStream.on('error', (err) => {
    console.error('Change stream error:', err)
    // Reconnect with resume token
    setTimeout(() => watchOrderChanges(), 5000)
  })
}
```

## Multi-Document Transactions

```typescript
async function transferFunds(
  fromAccountId: string,
  toAccountId: string,
  amount: number
): Promise<void> {
  const session = client.startSession()

  try {
    await session.withTransaction(async () => {
      const from = await db.accounts.findOne(
        { _id: new ObjectId(fromAccountId) },
        { session }
      )
      if (!from || from.balance < amount) {
        throw new Error('Insufficient funds')
      }

      await db.accounts.updateOne(
        { _id: new ObjectId(fromAccountId) },
        { $inc: { balance: -amount } },
        { session }
      )

      await db.accounts.updateOne(
        { _id: new ObjectId(toAccountId) },
        { $inc: { balance: amount } },
        { session }
      )

      await db.transactions.insertOne({
        from: fromAccountId,
        to: toAccountId,
        amount,
        createdAt: new Date()
      }, { session })
    })
  } finally {
    await session.endSession()
  }
}
```

## Checklist

- [ ] Embed for 1:1 and 1:few; reference for 1:many and many:many
- [ ] Follow ESR (Equality-Sort-Range) for compound index field order
- [ ] Use partial indexes to reduce index size on filtered queries
- [ ] Set TTL indexes for session/temp data auto-cleanup
- [ ] Use aggregation pipeline for analytics (not client-side loops)
- [ ] Change streams with resume tokens for crash-safe event processing
- [ ] Keep documents under 16MB (MongoDB limit)
- [ ] Use `explain()` to verify queries use indexes

## Anti-Patterns

- Unbounded arrays: reviews/comments embedded in parent (grows forever, hits 16MB)
- Missing indexes: full collection scans on frequently queried fields
- $lookup in hot paths: use denormalization, not joins, for read-heavy queries
- Storing related data in separate collections when always read together
- Using MongoDB as a relational database (normalize everything)
- Not using write concern `majority` for critical writes (data loss risk)

Attribution

vibeevalvibeeval
View sourceSee grades on GitHubMore from vibeeval →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Caveman

Terse caveman voice: answer first, fluff gone, every technical fact kept. Use for /caveman, "caveman mode", "talk like caveman", "be brief", "less tokens". Stays on until "stop caveman" or "normal mode".

1100021 votes

Hyperplan

Adversarial multi-agent planning skill. Self-orchestrates 5 hostile category members (unspecified-low, unspecified-high, deep, ultrabrain, artistry) via team-mode for ruthless cross-critique debate, distills only the defensible insights, then MANDATORILY hands the distilled insight bundle to the `plan` agent for executable plan formalization. Use when planning needs maximum rigor and surfacing of weak assumptions, blind spots, and over-engineering. Triggers: 'hyperplan', 'hpp', '/hyperplan', ...

698461 votes

Writing Skills

Create and manage Claude Code skills in HASH repository following Anthropic best practices. Use when creating new skills, modifying skill-rules.json, understanding trigger patterns, working with hooks, debugging skill activation, or implementing progressive disclosure. Covers skill structure, YAML frontmatter, trigger types (keywords, intent patterns), UserPromptSubmit hook, and the 500-line rule. Includes validation and debugging with SKILL_DEBUG. Examples include rust-error-stack, cargo-dep...

3931 votes

Mcp Code Execution

Routes multi-tool workflows through MCP servers for large datasets and pipelines. Use when Bash tool overhead is limiting throughput on data-heavy tasks.

3421 votes

catchup

Recovers the conversation and failed tool calls of a previous Codex, Amp, Claude Code, Antigravity, Cline, Copilot CLI, Cursor, DeepSeek Harness, Grok Build, Kimi, OpenCode, Pi Agent, or ZCode session. Use when the user says "catch up", "what did the last session do", "get me up to speed", "I switched agents", asks to recover/summarize a previous session before continuing, or asks to diagnose or report a catchup failure. Do NOT use for the current conversation, git history, or any non-agent log.

741 votes
View all in ai-agents →