Work with MongoDB in Node.js using Mongoose ODM for schema design, CRUD operations, relationships, and advanced queries
Work with MongoDB in Node.js using Mongoose ODM for schema design, CRUD operations, relationships, and advanced queries. Use when building Node.js applications that need flexible document-based data storage with schema validation and relationships.
/plugin marketplace add pluginagentmarketplace/custom-plugin-nodejs/plugin install nodejs-developer-plugin@pluginagentmarketplace-nodejsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlreferences/GUIDE.mdscripts/helper.pyMaster MongoDB database integration in Node.js with Mongoose, the elegant object modeling library.
Connect and CRUD in 4 steps:
npm install mongoosemongoose.connect(uri)const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on('connected', () => {
console.log('MongoDB connected');
});
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Name is required'],
trim: true,
minlength: 3,
maxlength: 50
},
email: {
type: String,
required: true,
unique: true,
lowercase: true
},
age: {
type: Number,
min: 18,
max: 120
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user'
}
}, {
timestamps: true // createdAt, updatedAt
});
const User = mongoose.model('User', userSchema);
// Create
const user = await User.create({
name: 'John Doe',
email: 'john@example.com'
});
// Read
const users = await User.find({ age: { $gte: 18 } });
const user = await User.findById(id);
const user = await User.findOne({ email: 'john@example.com' });
// Update
const updated = await User.findByIdAndUpdate(
id,
{ name: 'Jane Doe' },
{ new: true, runValidators: true }
);
// Delete
await User.findByIdAndDelete(id);
await User.deleteMany({ age: { $lt: 18 } });
const postSchema = new mongoose.Schema({
title: String,
content: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
// Populate relationship
const post = await Post.findById(id).populate('author');
// post.author is now full user object
userSchema.index({ email: 1 }, { unique: true });
userSchema.index({ name: 1, age: -1 });
userSchema.pre('save', async function(next) {
if (this.isModified('password')) {
this.password = await bcrypt.hash(this.password, 10);
}
next();
});
userSchema.virtual('fullName').get(function() {
return `${this.firstName} ${this.lastName}`;
});
const stats = await User.aggregate([
{ $match: { age: { $gte: 18 } } },
{ $group: {
_id: '$role',
count: { $sum: 1 },
avgAge: { $avg: '$age' }
}},
{ $sort: { count: -1 } }
]);
// Comparison
User.find({ age: { $gt: 18 } }) // Greater than
User.find({ age: { $gte: 18 } }) // Greater or equal
User.find({ age: { $lt: 65 } }) // Less than
User.find({ age: { $lte: 65 } }) // Less or equal
User.find({ age: { $ne: 30 } }) // Not equal
// Logical
User.find({ $and: [{ age: { $gte: 18 } }, { age: { $lte: 65 } }] })
User.find({ $or: [{ role: 'admin' }, { role: 'moderator' }] })
// Array
User.find({ tags: { $in: ['node', 'mongodb'] } })
User.find({ tags: { $nin: ['deprecated'] } })
// Regex
User.find({ email: /gmail\.com$/ })
lean() for read-only queries (better performance)Use MongoDB with Mongoose when:
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.