80%
MongoDB Documents, schema design, queries, aggregation, indexes0/26 questions known
1. Documents and collections
DefinitionMongoDB is a document database: JSON-like documents (BSON) in collections, with a flexible schema, rich queries, aggregation pipelines and horizontal scaling (sharding).
database: database
table: collection
row: document
column: field
2. Embed or reference?
data is read together (order + items): data is shared by many documents (a product)
one-to-few, bounded size: one-to-many unbounded (comments, logs)
the child has no life of its own: the child is queried on its own
"Design for your queries": model documents around how the app reads them.
Document size limit: 16 MB — never grow arrays without bound.
Duplicate small, rarely changing fields (customer name on an order) to avoid lookups.
3. Queries and updates
Comparison: $eq $ne $gt $gte $lt $lte $in $nin; logic: $and $or $not; arrays: $elemMatch, $all, $size.
Update operators: $set $unset $inc $push $pull $addToSet; upsert: true inserts if missing.
Single-document updates are atomic; multi-document changes need a transaction (replica set).
Common mistakeNoSQL injection: passing req.body straight into a query lets { "$gt": "" } match everything.
4. Aggregation pipeline
FlowEach stage transforms the documents and passes them on$match→$lookup→$unwind→$group→$sort→$project→$limit
5. Indexes and scaling
Every collection has an _id index; add indexes for your query filters and sorts.
Compound index field order: Equality → Sort → Range (the ESR rule).
Special indexes: unique, TTL (auto-expire sessions), text, geospatial, partial.
Check with .explain("executionStats"): you want IXSCAN, not COLLSCAN.
6. Mongoose
ReviseModel documents around queries: embed what is read together and bounded, reference what is shared or unbounded.
Most asked (8)
QWhat is MongoDB? Explain documents, collections and BSON.MongoDB is a document database: it stores data as JSON-like documents instead of rows in tables.
QHow is MongoDB different from a SQL database? When would you choose it?Terms map roughly as table → collection, row → document, column → field, join → $lookup or embedding.
QShow the basic CRUD operations in MongoDB.- Create: insertOne, insertMany - Read: find (returns a cursor) and findOne, with a filter and an optional projection to choose fields -…
QWhat query operators do you use most?- Comparison: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin - Logical: $and (implicit when you list several fields), $or, $not, $nor - Element…
QEmbedding vs referencing: how do you decide?Embed related data inside the parent document when it is read together, belongs to the parent, and is bounded in size: an order's line…
QWhat is the aggregation pipeline? Explain the common stages.The aggregation pipeline processes documents through a series of stages, each transforming the output of the previous one, like Unix pipes.
QWhat types of indexes does MongoDB support?Indexes are B-trees that let queries avoid a collection scan (COLLSCAN).
QWhat is Mongoose and why use it?Mongoose is an ODM (Object Document Mapper) for Node.js on top of the MongoDB driver.