Agent Skills › AjayIrkal23/agentic-mercy-10x › mongoose-patterns

mongoose-patterns

GitHub

提供Node.js Mongoose与MongoDB的最佳实践,涵盖高效查询、索引优化、聚合管道处理、模式定义及事务写入等开发规范。

skills/mongoose-patterns/SKILL.md AjayIrkal23/agentic-mercy-10x

Trigger Scenarios

需要编写Mongoose数据访问层代码 优化MongoDB查询性能或设计索引 构建或修改数据库Schema结构

Install

npx skills add AjayIrkal23/agentic-mercy-10x --skill mongoose-patterns -g -y
More Options

Use without installing

npx skills use AjayIrkal23/agentic-mercy-10x@mongoose-patterns

指定 Agent (Claude Code)

npx skills add AjayIrkal23/agentic-mercy-10x --skill mongoose-patterns -a claude-code -g -y

安装 repo 全部 skill

npx skills add AjayIrkal23/agentic-mercy-10x --all -g -y

预览 repo 内 skill

npx skills add AjayIrkal23/agentic-mercy-10x --list

SKILL.md

Frontmatter
{
    "name": "mongoose-patterns",
    "metadata": {
        "schema": 1,
        "category": "backend",
        "surfaces": [
            "backend"
        ],
        "triggers": {
            "intents": [],
            "keywords": [
                "mongoose",
                "mongodb",
                "mongo query",
                "mongoose schema",
                "mongoose model",
                "mongoose lean",
                "use lean",
                "projection",
                "aggregate pipeline",
                "aggregation pipeline",
                "objectid",
                "compound index",
                "slow mongo query",
                "soft delete",
                "backfill",
                "explain plan"
            ]
        },
        "platforms": [
            "linux",
            "darwin",
            "windows"
        ]
    },
    "description": "Mongoose and MongoDB patterns for Node services: lean reads with projections, index-backed filters and sorts, ObjectId casting in aggregation pipelines, timestamps, soft-delete filters and safe one-off backfills.",
    "when_to_use": "Use when writing or reviewing Mongoose models, queries, aggregations, indexes or data backfills, or when a Mongo query is slow."
}

Mongoose patterns

The project contract wins: read the repo's CLAUDE.md / CODEX.md / server_docs/ first (site-sync-vista: tenant scope from req.user, .lean() + projection, index-backed filters, { timestamps: true }, AppError for failures). These are the defaults when the repo is silent.

Reads

  • Read paths use .lean() and an explicit projection: Model.find(filter, "name status updatedAt").lean(). Hydrated documents are for code that calls save() or document methods.
  • Every query is tenant-scoped from the authenticated user, never from a client-sent companyId: { companyId: req.user.companyId, ... } (site-sync-vista derives it with resolveHomeCompanyId).
  • Lists paginate server-side: sort on a whitelisted key, then skip/limit (or a range cursor on _id/createdAt for deep pages), and countDocuments with the same filter. Never fetch everything and slice in JS.
  • populate costs one extra query per path; for lists, prefer a projection on the populate (populate({ path: "owner", select: "name" })) or a $lookup in one aggregation.

Indexes

  • Every field in a list filter, sort or $match is backed by an index. Compound order: equality fields first, then sort fields, then range fields (ESR).
  • Declare indexes on the schema (schema.index({ companyId: 1, status: 1, updatedAt: -1 })) and let the repo's index-management step build them; do not call createIndex from request code.
  • Check a suspect query with explain("executionStats"): IXSCAN and totalDocsExamined close to nReturned is healthy; COLLSCAN on a large collection needs an index.

Aggregation

  • Mongoose casts query filters but not aggregation pipelines. Cast ids and dates yourself: { $match: { companyId: new Types.ObjectId(companyId), createdAt: { $gte: new Date(from) } } }. A string id in $match silently matches nothing.
  • Put $match (index-backed) first, $project early to drop wide fields, $limit before $lookup when the result is paged.
  • Validate an id before casting (Types.ObjectId.isValid(id)) and throw the repo's 400 error instead of letting a cast error escape as 500.

Schemas

  • { timestamps: true } on every schema (site-sync-vista non-negotiable 8).
  • Soft delete = a field (deletedAt or isDeleted) plus a filter on every read and count. Prefer one shared filter helper or a query middleware over remembering it per call; add the field to the relevant compound indexes.
  • Enumerations: enum on the schema path and the same values in the route schema.
  • Keep models thin: statics or services own logic, not pre-save hooks with side effects.

Writes and backfills

  • Updates are atomic operators ($set, $inc, $push) with the tenant in the filter; findOneAndUpdate(..., { new: true, runValidators: true }) when you need the result.
  • Multi-document invariants use a session/transaction only when the deployment is a replica set; say so instead of assuming.
  • One-off backfills: a script that is idempotent (filter on "not yet migrated"), batched (bulkWrite in chunks of a few hundred), dry-run by default, and logs counts. Run it only with the user's OK against a real database.

Tests

Use the repo's existing setup (vitest with an in-memory Mongo or mocked models). Assert on the filter and projection a service builds, the tenant scope, and the cast types in pipelines.

Not this skill

SQL / Postgres (postgres-patterns), API envelope design (api-contract-standards), general service layering (service-layer-standards).

Version History

  • 2315578 Current 2026-10-18 19:22

Same Skill Collection

skills/agentation-react/SKILL.md
skills/baseline-ui/SKILL.md
skills/brand/SKILL.md
skills/context7-mcp/SKILL.md
skills/create-design-md/SKILL.md
skills/debug-broken-ui/SKILL.md
skills/design-system-compliance/SKILL.md
skills/design-taste-frontend/SKILL.md
skills/design/SKILL.md
skills/docker-compose/SKILL.md
skills/ensure-repo-docs/SKILL.md
skills/expo-react-native/SKILL.md
skills/false-green-tests/SKILL.md
skills/fastify-patterns/SKILL.md
skills/fixing-accessibility/SKILL.md
skills/fixing-metadata/SKILL.md
skills/fixing-motion-performance/SKILL.md
skills/grill-with-docs/SKILL.md
skills/higgsfield-brandkit/SKILL.md
skills/higgsfield-video-explainer/SKILL.md
skills/higgsfield-youtube-thumbnail/SKILL.md
skills/idea-refine/SKILL.md
skills/img2threejs/SKILL.md
skills/improve-ui/SKILL.md
skills/invoke-audit/SKILL.md
skills/invoke-clean/SKILL.md
skills/invoke-debug/SKILL.md
skills/invoke-design/SKILL.md
skills/invoke-docs/SKILL.md
skills/invoke-impl/SKILL.md
skills/invoke-plan/SKILL.md
skills/invoke-refactor/SKILL.md
skills/invoke-review/SKILL.md
skills/invoke-security/SKILL.md
skills/invoke-session/SKILL.md
skills/invoke-spec/SKILL.md
skills/invoke-status/SKILL.md
skills/invoke-test/SKILL.md
skills/invoke-update/SKILL.md
skills/invoke-verify/SKILL.md
skills/invoke/SKILL.md
skills/kokonutui/SKILL.md
skills/mmx-cli/h3-video/SKILL.md
skills/modern-web-guidance/SKILL.md
skills/no-ai-slop/SKILL.md
skills/slides/SKILL.md
skills/test-error-states/SKILL.md
skills/threejs-r3f/SKILL.md
skills/to-issues/SKILL.md

Metadata

Files
0
Version
2315578
Hash
45d7cd88
Indexed
2026-10-18 19:22

홈 - 위키
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-10-20 06:01
浙ICP备14020137号-1