Mongoose is an ODM library that models data for MongoDB.
Getting Started
What is Node.js?
Modules & NPM
Modules & NPM
Process & Environment
Process & Environment Variables
Caching & Performance
Redis Caching
GraphQL with Node.js
GraphQL Basics
Node.js
Intermediate
FREE
MongoDB with Mongoose
Lesson 2 of 17
Intermediate
Interactive
Syntax
NODEJS
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: { type: String, required: true }, email: { type: String, unique: true } }); const User = mongoose.model('User', userSchema);
Mongoose Schema and Model
NODE
const mongoose = require("mongoose"); const userSchema = new mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true, unique: true }, age: Number, createdAt: { type: Date, default: Date.now } }); const User = mongoose.model("User", userSchema); // Create async function createUser() { const user = await User.create({ name: "Alice", email: "alice@example.com", age: 25 }); console.log(user); } // Find async function findUsers() { const users = await User.find({ age: { $gte: 18 } }); console.log(users); }
Practice
1
Exercise
Practice this concept.
Answer
Write the code as shown above.
Quick Quiz
1
What did you learn?
This covers the basics.
Interview Questions
It is a fundamental Node.js feature.