Lesson 17 / 22
Mongoose Schema & Model
Add structure on top of MongoDB with Node.js.
Defining a schema
A Mongoose schema declares expected fields and types; model() turns it into a class you can use to query.
const { Schema, model } = require("mongoose");
const userSchema = new Schema({
name: String,
email: String,
age: Number
});
const User = model("User", userSchema);Creating & saving a document
Models give you familiar, promise-based methods like create() and .save().
const user = await User.create({
name: "Ada Lovelace",
email: "ada@example.com",
age: 36
});Structure on a schema-less DB
MongoDB itself doesn't enforce a schema — Mongoose adds that structure at the application layer, in your Node.js code.