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.
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 MongoDB interview questions
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 pagedb.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 pagedb.jobs.createIndex({ status: 1, postedAt: -1 })Checked against: MongoDB Manual: indexes
Intermediate MongoDB interview questions
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 pagedb.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 pagedb.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 pagedb.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 MongoDB interview questions
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 pagesh.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 pagedb.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 pagedb.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 pagedb.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })Checked against: MongoDB Manual: TTL indexes
How to practice with this page
- 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.
Related interview prep and tools
The relational alternative, checked against the docs.
Open →QuestionsMySQL interview questionsInnoDB, indexes and replication.
Open →QuestionsSystem design interview questionsChoosing data stores at scale.
Open →QuestionsNode.js interview questionsA common back end for MongoDB.
Open →Free toolResume summary generatorDraft a summary for a Backend Developer role; the title is filled in, you add your experience.
Open →Free toolLinkedIn headline generatorWrite a headline for a Backend Developer search.
Open →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.
Questions people ask
What MongoDB topics come up often in interviews?
Is MongoDB better than a relational database?
Were these commands run?
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.
- 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.
- MongoDB Manual: aggregation (read 2026-11-15)
- MongoDB Manual: compound indexes (read 2026-11-15)
- MongoDB Manual: documents (read 2026-11-15)
- MongoDB Manual: embedded data (read 2026-11-15)
- MongoDB Manual: the ESR guideline (read 2026-11-15)
- MongoDB Manual: explain results (read 2026-11-15)
- MongoDB Manual: indexes (read 2026-11-15)
- MongoDB Manual: read concern (read 2026-11-15)
- MongoDB Manual: referenced data (read 2026-11-15)
- MongoDB Manual: replication (read 2026-11-15)
- MongoDB Manual: sharding (read 2026-11-15)
- MongoDB Manual: shard keys (read 2026-11-15)
- MongoDB Manual: TTL indexes (read 2026-11-15)
- MongoDB Manual: transactions (read 2026-11-15)
- MongoDB Manual: schema validation (read 2026-11-15)
- MongoDB Manual: write concern (read 2026-11-15)