70 Progressive Questions (Easy to Advanced + Scenarios)
70 comprehensive interview questions spanning database basics, Mongoose ODM abstractions, advanced aggregation pipelines, system architecture, performance tuning, and real-world scenario challenges.
Q1: What is MongoDB and how does it differ from traditional RDBMS?
MongoDB is a source-available, document-oriented NoSQL database that stores data as flexible BSON (Binary JSON) documents. Unlike traditional RDBMS (which uses structured tables, fixed schemas, and SQL joins), MongoDB provides flexible dynamic schemas, high availability via replica sets, and native horizontal scaling through sharding.
- Dynamic dynamic schemas vs. fixed relational tables.
- Stores data in BSON format with native nested objects and arrays.
Which data format does MongoDB use internally to store and query documents?
Q2: What is Mongoose and why use it over the native MongoDB driver?
Mongoose is an Object Data Modeling (ODM) library for Node.js. While the native driver provides direct low-level CRUD access, Mongoose adds structure by providing schema validation, type casting, middleware hooks (pre/post), dynamic virtuals, and populated document references.
- Mongoose provides an ODM layer with schema enforcement.
- Includes auto-casting, middleware hooks, populate references, and validation.
What is the primary role of Mongoose in a Node.js application?
Q3: What is BSON and how does it extend standard JSON?
BSON stands for Binary JSON. It is a binary-encoded serialization format used by MongoDB to store data and execute operations. BSON extends JSON by adding support for extra data types (e.g., ObjectId, Date, BinData, Decimal128, Long) and enables faster traversal and indexing by prefixing fields with byte lengths.
- Extends JSON with native types like Date, ObjectId, and 64-bit Int.
- Includes length prefixes for fast traversal.
Which of the following data types is supported natively in BSON but NOT in standard JSON?
Q4: Explain the structure of a MongoDB ObjectId.
An ObjectId is a 12-byte BSON value designed to guarantee uniqueness across distributed systems without a central authority:
- 4 bytes: Timestamp value (seconds since epoch).
- 5 bytes: Random value unique to the machine and process.
- 3 bytes: Incrementing counter initialized to a random value.
- Total size: 12 bytes.
- First 4 bytes encode a UTC timestamp, allowing natural sorting by creation time.
How many bytes does a standard MongoDB ObjectId consume?
Q5: What is the difference between a Mongoose Schema and a Mongoose Model?
A Schema defines the structure, default values, validators, and middleware hooks for documents within a collection. A Model is a compiled constructor built from a Schema that provides the concrete interface to perform database CRUD operations (User.find(), User.create()).
const mongoose = require('mongoose');
// 1. Define Schema (Blueprint)
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, unique: true }
});
// 2. Compile Model (Database Interface)
const User = mongoose.model('User', userSchema);- Schema = Blueprint & Rules.
- Model = Compiled class providing CRUD access methods.
Which Mongoose call creates the database query interface from a defined schema?
Q6: When should you embed documents vs reference them in MongoDB?
- Embed (Denormalization): Use when data has a 1-to-1 or 1-to-Few relationship, is accessed together frequently, and data doesn't grow unbounded (e.g., user address list).
- Reference (Normalization): Use when data has a 1-to-Many (unbounded) or Many-to-Many relationship, data is updated independently, or document size approaches the 16MB limit.
- Embed for atomic updates and high-speed read joins.
- Reference for large, unbounded collections or shared entities.
Which pattern is recommended for storing a blog post's tags (5 to 10 tags per post)?
Q7: What is the maximum document size limit in MongoDB, and how do you store larger files?
The maximum BSON document size limit is 16 Megabytes. This prevents individual documents from overloading RAM or network bandwidth during retrieval. To store files or documents exceeding 16MB, MongoDB provides GridFS, which splits files into 255KB chunks across two collections (fs.files and fs.chunks).
- 16MB maximum single BSON document limit.
- Use GridFS to automatically chunk and stream large files.
Which built-in MongoDB feature supports storing single files exceeding 16MB?
Q8: What are the basic CRUD operations in Mongoose?
- Create:
Model.create({ name: 'Alice' })orconst doc = new Model(); await doc.save(); - Read:
Model.find({ age: { $gte: 18 } }),Model.findOne(),Model.findById() - Update:
Model.updateOne({ _id }, { $set: { status: 'active' } }),Model.findByIdAndUpdate() - Delete:
Model.deleteOne({ _id }),Model.findByIdAndDelete()
save()triggers validation and pre/post save hooks.updateOne/updateManybypass document instance hooks unless explicitly configured.
Which method instantiates a document and triggers Mongoose pre-save middleware hooks?
Q9: Explain fundamental comparison operators in MongoDB ($eq, $gt, $in, $nin).
Comparison operators match field values against criteria:
$gt/$gte: Greater than / Greater than or equal to.$lt/$lte: Less than / Less than or equal to.$in: Matches any value specified in an arraystatus: { $in: ['active', 'pending'] }.$nin: Matches values not present in the specified array.
$inevaluates array matches in a single query pass.- Combine operators for bounded range searches
{ age: { $gte: 18, $lte: 65 } }.
Which operator finds documents where a status field equals EITHER 'active' OR 'verified'?
Q10: How do logical query operators ($and, $or, $nor, $not) work?
Logical operators join multiple clause conditions:
$or: Returns documents matching at least one clause:{ $or: [{ role: 'admin' }, { points: { $gt: 100 } }] }.$and: Joins conditions implicitly or explicitly when querying the same field multiple times.$nor: Returns documents failing all listed conditions.
- Implicit
$andoccurs when comma-separating conditions in a query object. - Use explicit
$andwhen targeting the same operator key multiple times.
When is an explicit $and operator required in a MongoDB query?