本文へ移動
cccskills
無料GitHub で公開

mongodb-aggregation

Use this skill when writing aggregation pipelines, grouping, joining collections, or optimizing slow MongoDB queries. Trigger words: 集計, aggregate, パイプライン, $group, $lookup, $match, 結合, グループ化, 遅いクエリ, explain.

インストール方法を見る

含まれるファイル(1)

  • SKILL.md2.3 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

MongoDB Aggregation

いつ使うか

  • 集計(合計・件数・平均)やグループ化が必要なとき。
  • 複数コレクションを結合($lookup)するとき。
  • 単純な find では取れない加工済みデータが欲しいとき。
  • クエリが遅く、最適化したいとき。

やること

  • パイプラインは早い段階で絞る:$match と $project をできるだけ前に置く。
  • $match をインデックスが効く形(先頭で等価条件)にする。
  • $lookup は件数を絞ってから。N+1 を避けるが、巨大結合は分割も検討。
  • 必ず .explain('executionStats') で IXSCAN を確認。COLLSCAN は赤信号。
  • 読み取り目的なら結果を plain object として扱う。

守るルール

  • ✅ $match / $project を最前段に置きデータ量を減らす。
  • ✅ ソート対象フィールドにインデックス(ESR ルール)。
  • ✅ .explain('executionStats') で IXSCAN 確認を習慣化。
  • ✅ ユーザー入力は Zod 検証後にパイプラインへ。
  • ❌ $match を最後に置いて全件処理してから絞る。
  • ❌ ユーザー入力をそのまま $where / 演算子オブジェクトに展開しない。

典型例(コード片)

// ロール別の月間投稿数(直近 30 日)
const since = new Date(Date.now() - 30 * 864e5);
const rows = await PostModel.aggregate([
  { $match: { createdAt: { $gte: since } } },        // 先に絞る(インデックス)
  { $group: { _id: '$authorId', count: { $sum: 1 } } },
  { $sort: { count: -1 } },
  { $limit: 20 },
  { $lookup: { from: 'users', localField: '_id', foreignField: '_id', as: 'author' } },
  { $project: { count: 1, 'author.name': 1 } },       // 必要列だけ
]);

アンチパターン

  • パイプライン末尾で $match し、全件を集計してから捨てる。
  • $lookup で巨大コレクションを無制限結合してメモリ超過。
  • explain を見ずに「動いたから OK」とする。

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

api-routes-hardening

無料日本語概要

Use this skill when creating or reviewing Next.js Route Handlers (app/api), adding rate limiting, input validation, or auth to API endpoints. Trigger words: API ルート, Route Handler, app/api, エンドポイント, レート制限, 入力検証, CORS, ヘッダー, 認証付き API, hardening.

Akira-Papa/claude-code-nextjs-mongo-template-162026年5月24日 更新

atlas-search-vector

無料日本語概要

Use this skill when implementing full-text search, autocomplete, or vector/semantic search and RAG with MongoDB Atlas. Trigger words: 全文検索, あいまい検索, オートコンプリート, ベクトル検索, セマンティック検索, 類似検索, Atlas Search, $search, $vectorSearch, RAG, embedding.

Akira-Papa/claude-code-nextjs-mongo-template-162026年5月24日 更新

auth-better-auth

無料日本語概要

Use this skill when implementing sign-in, sessions, authorization checks, or protecting routes and Server Actions. Trigger words: 認証, ログイン, サインイン, セッション, 認可, 権限チェック, Better Auth, Auth.js, 保護, ミドルウェア, getSession.

Akira-Papa/claude-code-nextjs-mongo-template-162026年5月24日 更新

bootstrap

無料日本語概要

Use this skill when starting a new session, opening this project for the first time, or when you need to recall the project's tech stack, hard rules, and which other skills exist. Trigger words: セッション開始, このプロジェクト, 全体像, どのスキル, 何から, getting started, overview.

Akira-Papa/claude-code-nextjs-mongo-template-162026年5月24日 更新

ci-github-actions

無料日本語概要

Use this skill when setting up or modifying CI pipelines, GitHub Actions workflows, or automated checks (lint, typecheck, test, build) for this project. Trigger words: CI, GitHub Actions, ワークフロー, パイプライン, 自動テスト, lint チェック, typecheck, ビルド検証, .github/workflows.

Akira-Papa/claude-code-nextjs-mongo-template-162026年5月24日 更新

e2e-playwright

無料日本語概要

Use this skill when writing end-to-end tests, browser automation, or verifying user flows in a real browser. Trigger words: E2E, Playwright, ブラウザテスト, ユーザーフロー, 結合テスト, シナリオテスト, ログインテスト, getByRole, expect.

Akira-Papa/claude-code-nextjs-mongo-template-162026年5月24日 更新

Akira-Papa のスキルをすべて見る

このスキルの問題を報告する