const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
age: { type: Number, min: 0 },
});
const User = mongoose.model('User', userSchema);
const user = await User.create({ name: 'Alice', email: 'alice@example.com', age: 30 });
Topics
40
Advanced Node.js
Architecture & Design Patterns
Async Patterns
Authentication & Authorization
Authentication & Authorization (JWT, OAuth, Passport)
Background Jobs & Queues
Caching
Caching with Redis
Child Processes & Process Management
CLI Tools & Scripting with Node.js
Cloud & DevOps
Clustering & Worker Threads
Core Node.js Modules
Databases
Databases & ORMs (MongoDB/Mongoose, SQL/Sequelize)
Debugging & Diagnostics
Deployment & Process Managers (PM2)
Docker & Containerization for Node.js
Docker & Deployment
Email & Notifications
Environment Variables & Configuration
Error Handling
Event Loop & Non-blocking IO
Events & EventEmitter
Express & Middleware
File System & File Processing
File System (fs) Module
File Uploads & Media Processing
Git & Project Management
Global Objects & the process Object
GraphQL
GraphQL with Node.js
HTTP & HTTPS Modules
HTTP & Web Servers
Logging & Monitoring
Message Queues (RabbitMQ & Kafka)
Microservices Architecture with Node.js
Node.js Fundamentals & Runtime Architecture
Path & OS Modules
Performance Optimization & Profiling
Databases & ORMs (MongoDB/Mongoose, SQL/Sequelize)
5 questions found
Mongoose is an Object Document Mapper (ODM) for MongoDB, adding schema definition and validation (MongoDB itself is schemaless by default), typed model classes with instance and static methods, middleware/hooks (like running logic before or after a save), and built-in support for defining relationships between documents -- all on top of the lower-level, more manual raw MongoDB driver, which just gives you direct, unstructured database operations without any of this application-level structure.
Real-world example
A team migrating from the raw MongoDB driver to Mongoose immediately catches several data-quality bugs that had been silently accumulating, since Mongoose's schema validation now rejects documents missing required fields or containing values of the wrong type before they ever reach the database.
Core Node.js Modules;Testing with Jest
Mocha & the Node Test Runner
How does Sequelize define models and relationships for a relational (SQL) database in Node.js?
IntermediateSequelize defines models as JavaScript classes mapped to database tables, with each column's type and constraints declared explicitly, and relationships (one-to-many, many-to-many, one-to-one) declared via methods like hasMany(), belongsTo(), and belongsToMany() -- Sequelize then handles generating the underlying SQL joins and foreign key management automatically when you query related data, and can also auto-generate migrations reflecting schema changes.
const { DataTypes } = require('sequelize');
const User = sequelize.define('User', {
name: { type: DataTypes.STRING, allowNull: false },
email: { type: DataTypes.STRING, unique: true },
});
const Post = sequelize.define('Post', { title: DataTypes.STRING });
User.hasMany(Post);
Post.belongsTo(User);
const userWithPosts = await User.findByPk(1, { include: Post });
Real-world example
A blogging platform defines User and Post Sequelize models with a hasMany/belongsTo relationship, letting the application fetch a user along with all their posts in a single query via { include: Post }, without needing to hand-write the underlying SQL JOIN.
SQL Queries;Design Patterns in JavaScript
What is the N+1 query problem in the context of an ORM like Mongoose or Sequelize, and how do you avoid it?
AdvancedThe N+1 problem occurs when fetching a list of N parent records, then separately querying for each one's related data individually (N additional queries) instead of fetching everything in one or two efficient queries -- for example, fetching 100 blog posts and then looping through them to fetch each post's author separately triggers 101 total queries; the fix is eager loading (Mongoose's .populate(), Sequelize's 'include' option), which fetches the related data in the same or a single additional batched query.
// N+1 problem: 1 query for posts, then N queries for each author
const posts = await Post.find();
for (const post of posts) {
post.author = await User.findById(post.authorId); // triggers a separate query each time
}
// Fixed: a single additional query via populate()
const posts = await Post.find().populate('author');
Real-world example
A blog's homepage that was taking several seconds to load under moderate traffic is fixed by replacing a loop that fetched each post's author individually with a single .populate('author') call, collapsing what had been 101 separate database round-trips into just 2.
Performance Optimization & Profiling;Architecture & Design Patterns
How do you implement database transactions in Node.js using Sequelize to ensure multiple related writes succeed or fail together?
IntermediateSequelize's transaction() method wraps multiple operations so that either all of them commit together or, if any operation throws, all of them are automatically rolled back -- essential for maintaining data consistency in multi-step operations like transferring funds between two accounts, where partially applying only one side of the transfer would leave the data in an invalid, inconsistent state.
const transaction = await sequelize.transaction();
try {
await Account.decrement('balance', { by: 100, where: { id: fromId }, transaction });
await Account.increment('balance', { by: 100, where: { id: toId }, transaction });
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
Real-world example
A banking application wraps a funds transfer between two accounts in a Sequelize transaction, guaranteeing that if the increment on the receiving account fails for any reason after the decrement on the sending account already succeeded, the entire operation rolls back automatically rather than leaving money 'lost' from one account without arriving in the other.
Error Handling;Payments
What is a database migration, and how do tools like Sequelize's migration system or a standalone tool like Knex help manage schema changes over time?
IntermediateA migration is a version-controlled script describing an incremental change to a database schema (adding a column, creating a table, adding an index) -- migration tools track which migrations have already been applied to a given database and let a team apply pending changes consistently across development, staging, and production environments, and often support reversing ('down') a migration if it needs to be undone, providing an auditable, repeatable history of how the schema evolved over time.
// Sequelize migration file
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('Users', 'phoneNumber', { type: Sequelize.STRING, allowNull: true });
},
down: async (queryInterface) => {
await queryInterface.removeColumn('Users', 'phoneNumber');
},
};
// npx sequelize-cli db:migrate
Real-world example
A team adding a new required 'phoneNumber' column writes a migration rather than manually altering the production database table by hand, ensuring the exact same schema change is applied consistently and in the correct order across every developer's local database, staging, and production.
CI/CD
Publishing & Deployment;Architecture & Design Patterns