Advanced Node.js Interview QuestionsAdvancedConcept
Node.js · Question 83
What are MongoDB and Mongoose basics in Node.js?
Direct answer
MongoDB is a document NoSQL database storing BSON JSON-like docs; Mongoose is an ODM adding schemas, validation, and query helpers — connect with mongoose.connect(), define Schema/Model, use find/create/update; AceDevHub uses PostgreSQL + raw SQL instead for relational entitlements and audit.
MongoDB fits flexible document shapes and horizontal scaling — interviews still test Mongoose even when your stack uses SQL; know the mapping to AceDevHub's pg approach.
| Concept | Mongoose | AceDevHub (pg) |
|---|---|---|
| Connect | mongoose.connect(uri) | new Pool({ connectionString }) |
| Schema | new Schema({ title: String }) | SQL migrations + tables |
| Query | Model.find({ slug }) | pool.query('SELECT ... WHERE slug = $1') |
| Relations | populate() references | JOINs + foreign keys |
| Validation | Schema validators | Zod + DB constraints |
- Collections vs tables — documents in collections; no enforced schema unless Mongoose.
- _id — ObjectId primary key; default on insert.
- When Mongo — rapid prototyping, nested docs, content catalogs; when SQL — payments, entitlements, joins.
mongoose-basics.mjs
import mongoose from "mongoose";
const topicSchema = new mongoose.Schema({
slug: { type: String, required: true, unique: true },
title: String,
questionCount: Number,
});
const Topic = mongoose.model("Topic", topicSchema);
await mongoose.connect(process.env.MONGODB_URI);
const topic = await Topic.findOne({ slug: "nodejs-interview-questions" });