This MongoDB quiz contains multiple-choice questions with answers and explanations. It covers documents, collections, BSON, CRUD operations, query operators, indexes, aggregation, schema design, replication, sharding, and transactions.
Answer each question before opening its explanation. The quiz begins with MongoDB fundamentals and progresses to query design, performance, and distributed database concepts.
MongoDB Quiz Instructions and Score Guide
- Each MongoDB question has one correct answer.
- Assign one point for each correct response.
- Read the explanation after selecting an answer.
- For query-based questions, predict the result before using
mongosh.
| Score | Suggested MongoDB knowledge level |
|---|---|
| 21–25 | Strong understanding of core MongoDB concepts |
| 16–20 | Good foundation with a few areas to review |
| 10–15 | Basic knowledge; revisit queries and data modelling |
| 0–9 | Review MongoDB fundamentals before retaking the quiz |
MongoDB Documents, Collections, and BSON Quiz
1. What is the basic unit of data stored in MongoDB?
- Row
- Document
- Worksheet
- Graph node only
Answer
Correct answer: Document. MongoDB stores records as BSON documents made of field-value pairs. Documents are grouped into collections.
2. What is BSON in MongoDB?
- A binary representation of JSON-like documents
- A relational query language
- A MongoDB backup directory
- A JavaScript-only data format
Answer
Correct answer: A binary representation of JSON-like documents. BSON supports additional data types beyond standard JSON, including dates, binary data, decimal values, and ObjectId values.
3. Which field uniquely identifies a document in a MongoDB collection?
idprimaryKey_iddocumentId
Answer
Correct answer: _id. Every MongoDB document must contain a unique _id value. MongoDB creates an ObjectId automatically when the application does not provide one.
4. Which statement about MongoDB collections is correct?
- Every document must contain exactly the same fields.
- A collection stores BSON documents.
- A collection can contain only one document.
- A collection is the same as a MongoDB server.
Answer
Correct answer: A collection stores BSON documents. Documents in a collection may have different field structures unless schema validation rules are configured.
5. Which command selects or creates a database context in mongosh?
select database shopuse shopopen shopdb shop
Answer
Correct answer: use shop. The command switches the current database context. The database is created when data is first written to it.
MongoDB CRUD Operations Quiz Questions
6. Which method inserts one document into a collection?
insertOne()addRow()createRecord()appendDocument()
Answer
Correct answer: insertOne(). The method adds a single document to the selected collection.
7. Which MongoDB query returns products whose price is greater than 500?
db.products.find({ price: { $gt: 500 } })db.products.find({ price: > 500 })db.products.select(price > 500)db.products.get({ price: 500+ })
Answer
Correct answer: db.products.find({ price: { $gt: 500 } }). The $gt comparison operator matches values greater than the specified value.
8. Which method updates only the first document matching a filter?
replaceAll()updateOne()changeFirst()modify()
Answer
Correct answer: updateOne(). It updates one matching document. Use updateMany() when all documents matching the filter must be updated.
9. Which update operator assigns a new value to a field?
$set$assign$put$change
Answer
Correct answer: $set. The $set operator changes an existing field or creates the field when it does not exist.
10. What does deleteMany({ status: "inactive" }) do?
- Deletes the collection
- Deletes one inactive document
- Deletes all documents whose status is inactive
- Removes only the
statusfield
Answer
Correct answer: It deletes all documents whose status is inactive. The filter determines which documents are removed. An empty filter would match all documents in the collection.
MongoDB Query Operators and Array Queries Quiz
11. Which operator matches values contained in a supplied list?
$contains$in$within$list
Answer
Correct answer: $in. The $in operator matches documents where a field equals at least one value in the supplied array.
12. Which query matches documents where both conditions are true?
{ age: { $gte: 18 }, active: true }{ age: { $gte: 18 } OR active: true }{ $any: [age, active] }{ age: 18 + active: true }
Answer
Correct answer: { age: { $gte: 18 }, active: true }. Multiple field expressions in the same filter are combined with an implicit logical AND.
13. Which operator requires at least one expression to be true?
$or$all$andOnly$either
Answer
Correct answer: $or. The $or operator evaluates an array of expressions and matches a document when at least one expression is true.
14. Which query matches a document whose tags array contains both mongodb and database?
{ tags: { $all: ["mongodb", "database"] } }{ tags: ["mongodb" || "database"] }{ tags: { $both: ["mongodb", "database"] } }{ tags: "mongodb,database" }
Answer
Correct answer: { tags: { $all: ["mongodb", "database"] } }. The $all operator requires the array field to contain every specified value.
15. What does a projection such as { name: 1, price: 1, _id: 0 } do?
- Sorts by name and price
- Returns name and price while excluding
_id - Updates the name and price fields
- Creates indexes on three fields
Answer
Correct answer: It returns name and price while excluding _id. A projection controls which fields appear in the query result. The _id field can be excluded while other fields are included.
MongoDB Indexes and Query Performance Quiz
16. What is the primary purpose of a MongoDB index?
- To reduce the number of documents a query must examine
- To compress every document automatically
- To replace all collection validation rules
- To replicate data across regions
Answer
Correct answer: To reduce the number of documents a query must examine. An appropriate index can support filtering and sorting, although indexes also consume storage and add work to write operations.
17. Which method creates an ascending index on the email field?
db.users.createIndex({ email: 1 })db.users.addIndex("email ASC")db.users.index({ email: true })db.users.createKey({ email: 1 })
Answer
Correct answer: db.users.createIndex({ email: 1 }). A value of 1 specifies ascending index order, while -1 specifies descending order.
18. What does a unique index enforce?
- Every document must contain identical values.
- Indexed values cannot be duplicated, subject to the index definition.
- Only one index may exist in a collection.
- The collection becomes read-only.
Answer
Correct answer: Indexed values cannot be duplicated. A unique index rejects writes that would create duplicate indexed key values.
19. Which method helps inspect how MongoDB executes a query?
explain()debugQuery()showPlan()traceCollection()
Answer
Correct answer: explain(). Explain output can show the selected query plan, index usage, documents examined, and execution statistics depending on the requested verbosity.
20. Why can creating too many indexes be harmful?
- Indexes prevent all read queries.
- Each index consumes storage and must be maintained during writes.
- Indexes remove BSON type information.
- MongoDB permits only one index per database.
Answer
Correct answer: Each index consumes storage and must be maintained during writes. Indexes should be chosen from actual query patterns rather than added to every field.
MongoDB Aggregation, Replication, and Sharding Quiz
21. What is a MongoDB aggregation pipeline?
- A sequence of stages that transforms and analyses documents
- A backup process for BSON files
- A method for creating users
- A network protocol used only by replica sets
Answer
Correct answer: A sequence of stages that transforms and analyses documents. Each stage receives documents, processes them, and passes results to the next stage.
22. Which aggregation stage filters documents?
$match$group$project$outField
Answer
Correct answer: $match. The stage filters documents using query conditions. Placing selective $match stages early can reduce the amount of data processed by later stages.
23. What is the main purpose of a MongoDB replica set?
- To provide redundancy and automatic failover
- To remove all indexes
- To divide every document into fields
- To convert BSON into SQL
Answer
Correct answer: To provide redundancy and automatic failover. A replica set maintains copies of data across members. If the primary becomes unavailable, an eligible secondary can be elected as the new primary.
24. What is sharding used for in MongoDB?
- Distributing data across multiple shards
- Encrypting one field in a document
- Copying data only to a secondary node
- Replacing collections with tables
Answer
Correct answer: Distributing data across multiple shards. Sharding partitions a dataset across servers to support workloads that exceed the capacity of a single machine.
25. Which statement about MongoDB transactions is correct?
- MongoDB supports atomicity only for an entire database.
- Single-document writes are atomic, and multi-document transactions are also available.
- Transactions can be used only with text indexes.
- Every read automatically starts a multi-document transaction.
Answer
Correct answer: Single-document writes are atomic, and multi-document transactions are also available. Good MongoDB schema design often keeps data that must change together in one document, while transactions are available when atomic changes must span multiple documents or collections.
MongoDB Quiz Answer Key
| Question | Answer | Question | Answer | Question | Answer |
|---|---|---|---|---|---|
| 1 | Document | 10 | Deletes all matching documents | 19 | explain() |
| 2 | Binary JSON-like format | 11 | $in | 20 | Storage and write overhead |
| 3 | _id | 12 | Implicit AND filter | 21 | Document-processing stages |
| 4 | Stores BSON documents | 13 | $or | 22 | $match |
| 5 | use shop | 14 | $all | 23 | Redundancy and failover |
| 6 | insertOne() | 15 | Includes name and price | 24 | Distributes data |
| 7 | $gt query | 16 | Reduce documents examined | 25 | Atomic writes and transactions |
| 8 | updateOne() | 17 | createIndex({ email: 1 }) | ||
| 9 | $set | 18 | Prevents duplicate indexed values |
MongoDB Practice Query with Aggregation Pipeline
Consider a sales collection containing documents with category, quantity, and price fields. The following pipeline calculates revenue by category for completed sales.
db.sales.aggregate([
{
$match: { status: "completed" }
},
{
$group: {
_id: "$category",
totalRevenue: {
$sum: { $multiply: ["$quantity", "$price"] }
}
}
},
{
$sort: { totalRevenue: -1 }
}
])
The $match stage keeps completed sales, $group calculates total revenue for each category, and $sort orders the categories from highest to lowest revenue.
MongoDB Data Modelling Practice Question
An online store needs to retain the exact shipping address used for each order, even if the customer changes the address later. Which design is usually more suitable?
- Store only a reference to the customer’s current address.
- Embed a snapshot of the shipping address in the order document.
- Store all addresses in the database name.
- Create one collection for each order.
Answer
Correct answer: Embed a snapshot of the shipping address in the order document. The address forms part of the historical order record and should remain unchanged when the customer’s profile is updated.
MongoDB Practice Topics for Technical Interviews
MongoDB interview quizzes frequently test how database design choices affect correctness and performance. Review the behaviour of each feature rather than memorising method names alone.
- Practise filters using comparison, logical, element, and array operators.
- Review the difference between
updateOne(),updateMany(), andreplaceOne(). - Understand embedding, referencing, document growth, and access patterns.
- Learn how compound index field order relates to filtering and sorting.
- Read query plans with
explain()and compare keys examined with documents examined. - Practise aggregation stages such as
$match,$project,$unwind,$group,$lookup, and$sort. - Review replica-set elections, read preferences, write concerns, and sharding concepts.
- Know when single-document atomicity is sufficient and when a transaction is appropriate.
Frequently Asked Questions About the MongoDB Quiz
Is this MongoDB quiz suitable for beginners?
Yes. The opening questions cover documents, BSON, collections, _id, and basic CRUD methods. The later sections introduce indexes, aggregation, replication, sharding, and transactions.
Does this MongoDB quiz include answers and explanations?
Yes. Every multiple-choice question includes the correct answer and a concise explanation of the MongoDB concept being tested.
Which MongoDB topics are commonly tested in interviews?
Common topics include CRUD operations, schema design, aggregation pipelines, indexes, query plans, replication, sharding, consistency settings, transactions, and the trade-offs between embedding and referencing.
How can I practise MongoDB queries effectively?
Create a small dataset and write filters, updates, projections, sorts, and aggregation pipelines against it. Check the returned documents and use explain() to understand how indexes affect execution.
What should I study before attempting an advanced MongoDB quiz?
Study compound and partial indexes, aggregation optimisation, replica-set behaviour, shard-key selection, write concerns, read concerns, transactions, change streams, schema validation, and operational monitoring.
MongoDB Quiz Editorial QA Checklist
- Verify every MongoDB question has one unambiguous correct answer.
- Run query examples in a supported
mongoshenvironment before publication. - Confirm method names use current CRUD APIs such as
insertOne(),find(),updateOne(), anddeleteMany(). - Check that BSON, JSON, ObjectId, collection, and document terminology is used accurately.
- Review index explanations for both read benefits and write-maintenance costs.
- Ensure aggregation examples use valid stage syntax and field references.
- Distinguish replication for availability from sharding for data distribution.
- Confirm transaction statements do not overlook single-document atomicity.
- Ensure new query blocks use the correct PrismJS-compatible language class.
- Retest the answer-key numbering whenever quiz questions are edited or reordered.
TutorialKart.com