Skip to content
TryHired.
Interview prep · Technical · checked against the official docs

MongoDB interview questions: answers checked against the docs

The document database questions back-end and data roles get, answered in plain words and checked against the MongoDB manual.

Updated Nov 16, 2026 · Facts checked against the official documentation · questions: TryHired
Quick answer

MongoDB interviews cover the document model and BSON, embedding versus referencing, indexes and compound index order, explain plans, the aggregation pipeline, replica sets, sharding and shard keys, transactions, read and write concern, schema validation and TTL indexes. Below are 12 questions; each answer lists the MongoDB manual pages it was checked against.

Basic

Basic MongoDB interview questions

Advice

Basic1. How does MongoDB store data, and how is it different from a relational database?

MongoDB stores documents in collections. Documents are stored as BSON, a binary form of JSON with extra types such as dates and decimals, and documents in one collection can have different fields. Every document needs a unique, immutable _id that acts as its primary key, and a single document can be at most 16 MiB. Instead of joining tables, you often keep related data together in one document.

mongosh snippet · illustrative, not run on this page
db.jobs.insertOne({
  title: "Data Analyst",
  company: { name: "Acme", city: "Austin" },
  skills: ["SQL", "Python"],
  postedAt: new Date()
})

Checked against: MongoDB Manual: documents

Basic2. When do you embed documents, and when do you use references?

Embed when the related data is read together and belongs to the parent, such as an order's line items: one read returns everything. Use references (store the other document's _id) when the related data is large, shared by many documents, or changes independently, which avoids duplicating it; $lookup can join referenced data in an aggregation. Design around your application's queries.

Checked against: MongoDB Manual: embedded data, MongoDB Manual: referenced data

Basic3. Why do indexes matter in MongoDB?

Without an index, a query must scan every document in the collection. Indexes (B-trees) let MongoDB find matching documents directly and can also return results already sorted. Each index costs write time and memory, so index the fields your frequent queries filter and sort on.

mongosh snippet · illustrative, not run on this page
db.jobs.createIndex({ status: 1, postedAt: -1 })

Checked against: MongoDB Manual: indexes

Intermediate

Intermediate MongoDB interview questions

Advice

Intermediate4. How do you order the fields in a compound index?

Order matters. A compound index supports queries on its leading fields (its prefixes). MongoDB's ESR guideline helps for queries that combine filters and sorts: equality fields first, then sort fields, then range fields. For a query on status equal to a value, sorted by date, with a salary range, that gives { status: 1, postedAt: -1, salary: 1 }.

mongosh snippet · illustrative, not run on this page
db.jobs.find({ status: "open", salary: { $gte: 80000 } }).sort({ postedAt: -1 })
db.jobs.createIndex({ status: 1, postedAt: -1, salary: 1 })

Checked against: MongoDB Manual: compound indexes, MongoDB Manual: the ESR guideline

Intermediate5. How do you find out why a query is slow?

Run it with explain("executionStats") and read the winning plan: a COLLSCAN stage means no index was used, and comparing the documents examined with the documents returned shows how much extra work the query did. Then add or adjust an index, or change the query so it can use one.

mongosh snippet · illustrative, not run on this page
db.jobs.find({ status: "open" }).sort({ postedAt: -1 }).explain("executionStats")

Checked against: MongoDB Manual: explain results

Intermediate6. What is the aggregation pipeline?

A sequence of stages, each transforming the documents it receives: $match filters, $group aggregates, $project reshapes, $sort orders, $lookup joins another collection, $unwind expands arrays. Put $match and $sort early, so they can use indexes and later stages handle fewer documents.

mongosh snippet · illustrative, not run on this page
db.jobs.aggregate([
  { $match: { status: "open" } },
  { $group: { _id: "$company.city", openings: { $sum: 1 } } },
  { $sort: { openings: -1 } },
  { $limit: 5 }
])

Checked against: MongoDB Manual: aggregation

Intermediate7. How does replication work?

A replica set is a group of mongod processes holding the same data. One primary receives all writes; secondaries replicate the primary's oplog and apply the same operations. If the primary becomes unavailable, an eligible secondary is elected as the new primary, which gives automatic failover. Production replica sets typically have at least three members.

Checked against: MongoDB Manual: replication

Advanced

Advanced MongoDB interview questions

Advice

Advanced8. What is sharding, and how do you choose a shard key?

Sharding spreads a collection across several shards (each usually a replica set) for horizontal scaling; mongos routers send each query to the right shards using metadata held by config servers. The shard key decides how data is distributed. A good key has many distinct values, spreads writes evenly and appears in common queries. A value that only increases sends all new inserts to one shard (a hot spot); a key with few distinct values, or one very frequent value, can create jumbo chunks that cannot be split.

mongosh snippet · illustrative, not run on this page
sh.shardCollection("jobsdb.applications", { candidateId: "hashed" })

Checked against: MongoDB Manual: sharding, MongoDB Manual: shard keys

Advanced9. Does MongoDB support transactions?

Yes. A write to a single document is always atomic, which is why embedding related data often removes the need for transactions. Multi-document transactions are also available; they provide atomicity, consistency, isolation and durability according to the read and write concern you configure. They cost more than single-document writes, so the MongoDB docs say they should not replace a good schema design.

Checked against: MongoDB Manual: transactions

Advanced10. What are write concern and read concern?

Write concern sets how many members must acknowledge a write before it succeeds: w: "majority", the default in typical deployments, waits until more than half of the voting, data-bearing members have durably written it, so it survives a failover. Read concern sets which data a read can see: "local" returns the node's latest data even if it has not yet reached more than half of the members, which could be rolled back, while the "majority" level returns only data that more than half of the members have written.

mongosh snippet · illustrative, not run on this page
db.applications.insertOne(
  { candidateId: 42, jobId: 7 },
  { writeConcern: { w: "majority", wtimeout: 5000 } }
)

Checked against: MongoDB Manual: read concern, MongoDB Manual: write concern

Advanced11. How do you enforce a schema in MongoDB?

Collections are flexible by default, but you can add schema validation with a $jsonSchema validator: required fields, types and allowed values. You choose whether invalid writes are rejected or only logged, which helps when adding rules to existing data.

mongosh snippet · illustrative, not run on this page
db.createCollection("candidates", {
  validator: { $jsonSchema: {
    bsonType: "object",
    required: ["email"],
    properties: { email: { bsonType: "string" } }
  } }
})

Checked against: MongoDB Manual: schema validation

Advanced12. What is a TTL index?

A single-field index on a date field that tells MongoDB to delete documents automatically after a set number of seconds, or at a specific time stored in the field. It suits sessions, temporary tokens and logs. Deletion is done by a background task that runs every 60 seconds, so documents can remain briefly after they expire.

mongosh snippet · illustrative, not run on this page
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })

Checked against: MongoDB Manual: TTL indexes

Practice

How to practice with this page

Advice
  • Design a schema for one real feature and defend embed versus reference.
  • Read an explain plan and find the stage that examines the largest number of documents.
  • Write an aggregation pipeline that groups and sorts.
  • Be ready to compare MongoDB with a relational database for a given use case.
Next

Related interview prep and tools

Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.

FAQ

Questions people ask

What MongoDB topics come up often in interviews?
The document model and schema design (embedding versus referencing), indexes and the ESR guideline, explain plans, the aggregation pipeline, replica sets, sharding and shard keys, transactions, and read and write concerns.
Is MongoDB better than a relational database?
It depends on the data and the queries. MongoDB fits flexible, document-shaped data and scales out with sharding; relational databases fit strongly related data with complex joins and constraints. Interviewers look for that reasoning, not a winner.
Were these commands run?
No. There is no MongoDB server on the build machine, so the mongosh snippets are illustrations and no output is shown. Collection and field names are examples.
Method

How this page is built

TryHired wrote these questions and answers. Statements about the technology were checked against the official documentation pages listed below, saved when the page was built (2026-11-16). The code snippets are short illustrations and were not run on this page. The question list is general practice: it does not predict what a particular employer will ask.

What this page can't tell you
  • Interviewers ask different questions for different roles and levels.
  • Answers are short explanations, not complete references.
  • No MongoDB server was used; behavior follows the current MongoDB manual and can differ between versions and Atlas tiers.
Sources
  1. MongoDB Manual: aggregation (read 2026-11-15)
  2. MongoDB Manual: compound indexes (read 2026-11-15)
  3. MongoDB Manual: documents (read 2026-11-15)
  4. MongoDB Manual: embedded data (read 2026-11-15)
  5. MongoDB Manual: the ESR guideline (read 2026-11-15)
  6. MongoDB Manual: explain results (read 2026-11-15)
  7. MongoDB Manual: indexes (read 2026-11-15)
  8. MongoDB Manual: read concern (read 2026-11-15)
  9. MongoDB Manual: referenced data (read 2026-11-15)
  10. MongoDB Manual: replication (read 2026-11-15)
  11. MongoDB Manual: sharding (read 2026-11-15)
  12. MongoDB Manual: shard keys (read 2026-11-15)
  13. MongoDB Manual: TTL indexes (read 2026-11-15)
  14. MongoDB Manual: transactions (read 2026-11-15)
  15. MongoDB Manual: schema validation (read 2026-11-15)
  16. MongoDB Manual: write concern (read 2026-11-15)