mongodb-mongoose/SKILL.md
Use when working with MongoDB and Mongoose ODM in Node.js or TypeScript projects. Covers schema design, models, queries, aggregation pipelines, indexes, and connection management.
npx skillsauth add Heldinhow/awesome-opencode-dev-skills mongodb-mongooseInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
bun add mongoose
bun add -D @types/mongoose # if needed
// lib/mongodb.ts
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGODB_URI!
// Next.js / serverless: cache connection across hot reloads
let cached = (global as any).mongoose ?? { conn: null, promise: null }
export async function connectDB() {
if (cached.conn) return cached.conn
cached.promise ??= mongoose.connect(MONGODB_URI, {
bufferCommands: false,
})
cached.conn = await cached.promise
return cached.conn
}
// models/User.ts
import mongoose, { Schema, model, models, InferSchemaType } from 'mongoose'
const userSchema = new Schema({
name: { type: String, required: true, trim: true },
email: { type: String, required: true, unique: true, lowercase: true },
age: { type: Number, min: 0, max: 150 },
role: { type: String, enum: ['user', 'admin'], default: 'user' },
tags: [String],
address: {
street: String,
city: String,
country: { type: String, default: 'BR' },
},
createdAt: { type: Date, default: Date.now },
}, {
timestamps: true, // auto-adds createdAt, updatedAt
})
// Indexes
userSchema.index({ email: 1 }, { unique: true })
userSchema.index({ name: 'text' }) // text search
// Virtual
userSchema.virtual('displayName').get(function () {
return `${this.name} <${this.email}>`
})
// Type inference
export type User = InferSchemaType<typeof userSchema>
// Avoid re-compiling model in Next.js hot reload
export const UserModel = models.User ?? model('User', userSchema)
import { UserModel } from './models/User'
// Create
const user = await UserModel.create({ name: 'Alice', email: '[email protected]' })
// Find
const allUsers = await UserModel.find()
const user = await UserModel.findById(id)
const user = await UserModel.findOne({ email: '[email protected]' })
// Query operators
const users = await UserModel.find({
age: { $gte: 18, $lte: 65 },
role: { $in: ['user', 'admin'] },
name: { $regex: /alice/i },
})
// Select fields
const users = await UserModel.find().select('name email -_id')
// Sort, limit, skip
const users = await UserModel.find().sort({ createdAt: -1 }).limit(10).skip(20)
// Update
await UserModel.findByIdAndUpdate(id, { $set: { name: 'Alice Updated' } }, { new: true })
await UserModel.updateMany({ role: 'user' }, { $set: { verified: true } })
// Delete
await UserModel.findByIdAndDelete(id)
await UserModel.deleteMany({ role: 'banned' })
// Count
const total = await UserModel.countDocuments({ role: 'user' })
const postSchema = new Schema({
title: String,
author: { type: Schema.Types.ObjectId, ref: 'User' },
})
// Populate author
const posts = await PostModel.find().populate('author', 'name email')
const result = await UserModel.aggregate([
{ $match: { role: 'user' } },
{ $group: { _id: '$country', count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $limit: 10 },
])
const session = await mongoose.startSession()
try {
session.startTransaction()
await UserModel.create([{ name: 'Bob', email: '[email protected]' }], { session })
await OrderModel.create([{ userId: 'xxx' }], { session })
await session.commitTransaction()
} catch (err) {
await session.abortTransaction()
throw err
} finally {
session.endSession()
}
timestamps: true on every schematools
Implement WebSocket communication for real-time bidirectional client-server communication.
development
Implement webhook handlers for processing incoming events from external services.
development
Test web applications using Playwright for end-to-end browser testing.
development
Build production-quality HTML artifacts using React, Tailwind CSS, and shadcn/ui.