AceDevHub
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.

ConceptMongooseAceDevHub (pg)
Connectmongoose.connect(uri)new Pool({ connectionString })
Schemanew Schema({ title: String })SQL migrations + tables
QueryModel.find({ slug })pool.query('SELECT ... WHERE slug = $1')
Relationspopulate() referencesJOINs + foreign keys
ValidationSchema validatorsZod + 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" });