yoklainterview sim

MongoDB Database Interview Questions

450 verified MongoDB Database interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Mongodb AggregationDifficulty 1
In MongoDB, what is an aggregation pipeline?
  • aA single query operator that replaces find() entirely
  • bAn ordered sequence of stages, where each stage's output feeds the next stage's input
  • cA background thread that pre-computes indexes for faster reads
  • dA stored procedure written in JavaScript and executed on the client
Explanation:An aggregation pipeline is an array of stages (e.g. $match, $group, $project) passed to aggregate(); documents flow through the stages in order, each stage transforming the output of the previous one. It doesn't replace find() (a) — find() still exists for simple queries — it isn't an index-building thread (c), and it isn't a stored procedure (d).
Mongodb AggregationDifficulty 1
What does the $match stage do in an aggregation pipeline?
  • aFilters documents so only those matching the given condition pass to the next stage
  • bRenames every field in the document to the given target name
  • cJoins the current collection with another collection by a shared field
  • dGroups documents by a key and computes an accumulator per group
Explanation:$match filters the input documents using query-like criteria, letting only matching documents continue down the pipeline — conceptually similar to a WHERE clause or find()'s query argument. Renaming fields is $project/$addFields (b), joining is $lookup (c), and grouping is $group (d).
Mongodb AggregationDifficulty 2
db.orders.aggregate([
  { $group: { _id: "$status", total: { $sum: "$amount" } } }
])

What does the _id field inside $group control?
  • aIt is optional metadata and can be omitted from a $group stage
  • bIt always refers to the original MongoDB _id field of each document
  • cIt defines the grouping key — here, one output document per distinct status value
  • dIt sets the name of the output collection where grouped results are stored
Explanation:$group's _id expression is mandatory and defines what documents are grouped by — here "$status" means one result document per distinct status, each carrying the computed total. It is required, not optional (a); it does not have to reference the document's original _id (b), and $group doesn't write to a collection by itself — that's $out/$merge (d).
Mongodb AggregationDifficulty 1
What is the primary purpose of the $project stage?
  • aTo permanently delete unwanted fields from the underlying collection on disk
  • bTo create a new index on the fields listed in the stage
  • cTo filter out documents that don't satisfy a boolean condition
  • dTo reshape documents — include, exclude, or compute fields for the output
Explanation:$project reshapes each document: it can include existing fields, exclude them, rename them, or compute new fields from expressions — purely for the output of the pipeline. It never modifies the stored collection on disk (a), it doesn't create indexes (b, that's createIndex), and filtering documents is $match's job (c).
Mongodb AggregationDifficulty 2
{ $group: { _id: "$customerId", orderCount: { $sum: 1 } } }

What does { $sum: 1 } compute here?
  • aThe sum of every numeric field found anywhere in the document
  • bA count of documents in each group, since 1 is added once per document
  • cThe average order amount for each customer, expressed as an integer
  • dThe highest _id value observed among the grouped customer documents
Explanation:{ $sum: 1 } adds the literal value 1 for every document that falls into a group, which is the standard idiom for counting documents per group — equivalent to COUNT(*) ... GROUP BY. It does not sum arbitrary numeric fields (a), it isn't an average (c, that's $avg), and it has nothing to do with _id magnitude (d).
Mongodb AggregationDifficulty 1
What does the $unwind stage do to a document that has an array field?
  • aIt deconstructs the array, outputting one document per array element
  • bIt sorts the array's elements in ascending order in place
  • cIt removes the array field entirely from every document
  • dIt merges the array into a single comma-separated string field
Explanation:$unwind takes a document with an array field and outputs a separate document for each element of that array, with the field replaced by the single element value — useful before grouping or filtering on array contents. It doesn't sort in place (b), doesn't delete the field (c), and doesn't stringify the array (d).

Test yourself against the 2475-question Database bank.

Start interview