Home › MongoDB › MongoDB Windsurf rules

MongoDB Windsurf rules

Windsurf rules for MongoDB 8 apps with the Node.js driver: schema design, indexes, aggregation, and transactions.

WindsurfMongoDB

How to use

Save this as .windsurf/rules/mongodb.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.windsurf/rules/mongodb.mdDownload  
---
trigger: glob
globs: **/*.{ts,js}
---

# MongoDB (Node.js driver)

- One `MongoClient` per process, created at startup and reused (in serverless, cache it on `globalThis`). Connection string from env.
- Model data for the app's queries: embed data read together and bounded in size; reference (store ids) for unbounded or independently updated data. Documents must stay well under 16 MB.
- Type collections: `db.collection<Order>('orders')`. Validate writes with a zod schema in code and a `$jsonSchema` validator on the collection.
- Every frequent query has a supporting index (ESR rule: equality, sort, range). Create indexes in a migration/startup script, and check plans with `.explain('executionStats')` (look for `IXSCAN`, not `COLLSCAN`).
- Use projections to return only needed fields. Paginate with range queries on an indexed field (`{ _id: { $gt: lastId } }`), not big `skip`.
- Updates use operators (`$set`, `$inc`, `$push`) and atomic `findOneAndUpdate`; never read-modify-write the whole document.
- Multi-document consistency with `session.withTransaction()` (needs a replica set); keep transactions short.
- Never build queries from raw user objects (operator injection like `{ $gt: '' }`); pick fields explicitly and cast types.
- Use `ObjectId.createFromHexString` with validation for ids from URLs.

## Done when
Tests run against a real MongoDB (Testcontainers or `mongodb-memory-server`) and pass.

More: all MongoDB rules · all Windsurf rules

Related rules

Mongoose (MongoDB ODM) rules

Cursor rules for Mongoose 8 with TypeScript: typed schemas, indexes, lean queries, validation, and transactions.

CursorMongoDB

Redis Windsurf rules

Windsurf rules for using Redis (or Valkey) as cache, rate limiter, and queue: key design, TTLs, and atomicity.

WindsurfRedis / Valkey

Convex backend rules

Cursor rules for Convex backends: schema, validated queries/mutations/actions, indexes, and auth checks.

CursorConvex

Drizzle ORM rules

Cursor rules for Drizzle ORM with Postgres or SQLite: schema files, drizzle-kit migrations, and type-safe queries.

CursorDrizzle ORM

Firebase (modular SDK) rules

Cursor rules for Firebase web apps with the modular v11+ SDK, Firestore security rules, and emulator tests.

CursorFirebase

Neon serverless Postgres CLAUDE.md

CLAUDE.md for apps on Neon serverless Postgres: pooled connections, the serverless driver, branches, and migrations.

Claude Code (CLAUDE.md)Neon Postgres

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.