ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
MongoDB NoSQL Database Tutorial• 6 min read
Mongoose ODM: Schemas, Models & Validation
Connect Node.js and TypeScript applications to MongoDB with Mongoose schemas, virtual properties, and hooks.
Modeling with Mongoose
Mongoose provides schema enforcement, automatic type casting, lifecycle hooks (pre("save")), and reference population (populate()).
Mongoose Schema Definition
import mongoose, { Schema } from 'mongoose';
const UserSchema = new Schema({
email: { type: String, required: true, unique: true, lowercase: true },
passwordHash: { type: String, required: true },
role: { type: String, enum: ['user', 'admin'], default: 'user' }
}, { timestamps: true });
export const User = mongoose.model('User', UserSchema);ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which popular Node.js library acts as an Object Data Modeling (ODM) layer for MongoDB with schema validation?