MongoDB is flexible — it lets you store almost any document, in any shape. That flexibility is a strength until your data starts drifting: one user has an email field, another has Email , a third has neither. Mongoose fixes that by adding schemas to MongoDB, so your data has a shape your code can rely on.
Mongoose is an Object Data Modeling library for Node.js. You define a schema — the fields, their types, and their rules — and Mongoose turns it into a model you use to create, read, update, and delete documents. It also adds validation, middleware, and helpful methods on top of the raw driver.
Here is a complete example:
import mongoose from 'mongoose' ;
const userSchema = new mongoose . Schema ( { name : { type : String , required : true } , email : { type : String , required : true , unique : true } , age : { type : Number , min : 0 } , createdAt : { type : Date , default : Date . now } } ) ;
const User = mongoose . model ( 'User' , userSchema ) ;
await mongoose .…