MongoDB Associate Developer Exam — Questions and Answers
Question 1: What does the $type operator do in a MongoDB query?
- Creates a typed index on a field
- Converts a field to a different BSON type
- Matches documents where a field is of a specified BSON type (Correct answer)
- Returns the type of a field as a string
Correct answer: Matches documents where a field is of a specified BSON type
$type selects documents where the value of a field matches the specified BSON type.
Question 2: What does the 'approximation pattern' optimize in MongoDB?
- Reduces write operations by approximating counters instead of updating on every event (Correct answer)
- Approximate geospatial query results
- Approximate index size calculations
- Approximate query execution time estimates
Correct answer: Reduces write operations by approximating counters instead of updating on every event
The approximation pattern reduces write load by updating counters or statistics only periodically rather than on every event, trading perfect accuracy for performance.
Question 3: What is a key indicator that you should move from embedding to referencing in an existing schema?
- Query response times are under 1 ms
- The database is using WiredTiger storage engine
- The collection has fewer than 1,000 documents
- Embedded arrays are growing unboundedly, risking the 16 MB document limit (Correct answer)
Correct answer: Embedded arrays are growing unboundedly, risking the 16 MB document limit
When embedded arrays grow without bound — e.g., an activity log embedded in a user document — the document can eventually exceed the 16 MB BSON limit. This is a clear signal to switch to referencing, storing related records in a separate collection.
Question 4: What is a covered query in MongoDB?
- A query that can be satisfied entirely using an index without accessing documents (Correct answer)
- A query with full-text search coverage
- A query protected by access control
- A query that covers all documents in a collection
Correct answer: A query that can be satisfied entirely using an index without accessing documents
A covered query is fulfilled entirely by the index, meaning MongoDB never needs to read actual documents, resulting in very high performance.
Question 5: Which method creates an index on a MongoDB collection?
- collection.createIndex() (Correct answer)
- collection.buildIndex()
- collection.addIndex()
- collection.newIndex()
Correct answer: collection.createIndex()
createIndex() creates an index on the specified field(s) with the given options, or returns the existing index name if it already exists.
Question 6: In MongoDB, what does 'schema-on-read' mean?
- Collection schemas are loaded into memory on first access
- Schema validation is applied when reading documents
- Indexes are built lazily when first read
- The application interprets document structure at read time rather than enforcing it at write time (Correct answer)
Correct answer: The application interprets document structure at read time rather than enforcing it at write time
Schema-on-read means MongoDB stores any document structure and the application code is responsible for interpreting the data's shape when reading.
Question 7: ______ is the mongo shell command that lists database names.
- show dbs (Correct answer)
- show db
- show database
- none of the above
Correct answer: show dbs
The `show dbs` command is a standard utility in the MongoDB shell used to display a list of all available databases on the current MongoDB instance. It provides a quick overview of the databases present, along with their respective sizes. This command is essential for navigating and managing different databases within the shell environment.
Question 8: What is a hashed shard key in MongoDB and when should you use it?
- A shard key encrypted for security purposes
- A shard key that uses SHA-256 for uniqueness guarantees
- A compound shard key using a hash of multiple fields
- A shard key where MongoDB hashes the field values to distribute documents evenly, ideal for monotonically increasing fields (Correct answer)
Correct answer: A shard key where MongoDB hashes the field values to distribute documents evenly, ideal for monotonically increasing fields
A hashed shard key applies a hash function to the shard key values before distributing documents. This ensures even distribution even for monotonically increasing fields (like ObjectId or timestamps), preventing hot spots. The trade-off is that range queries are inefficient.
Question 9: What is document validation in MongoDB?
- Checking documents for duplicate _id values
- Validating that documents are syntactically correct JSON
- Enforcing schema rules on insert and update operations using validation expressions (Correct answer)
- Verifying document checksums after writes
Correct answer: Enforcing schema rules on insert and update operations using validation expressions
MongoDB's document validation uses JSON Schema or query expressions to enforce rules on documents during insert and update operations.
Question 10: What is the purpose of a TTL (Time-To-Live) index in MongoDB?
- Limits how long index builds can run
- Tracks the time it takes to traverse an index
- Sets the maximum age of index entries
- Automatically deletes documents after a specified time period (Correct answer)
Correct answer: Automatically deletes documents after a specified time period
A TTL index automatically removes documents from a collection after a specified number of seconds, useful for session data or logs.
Question 11: What does the $not operator do in a MongoDB query?
- Excludes a field from the projection
- Returns documents that don't match any query expression
- Checks if a field value is not null
- Inverts the effect of a single query expression (Correct answer)
Correct answer: Inverts the effect of a single query expression
$not inverts the result of a query expression, returning documents that do not match the expression.
Question 12: What is a transient transaction error in MongoDB and how should it be handled?
- An error that permanently invalidates the transaction; application must abort
- An error that commits a partial transaction
- A temporary error (e.g., write conflict or network issue) that can be resolved by retrying the entire transaction (Correct answer)
- An error caused by schema validation failure within the transaction
Correct answer: A temporary error (e.g., write conflict or network issue) that can be resolved by retrying the entire transaction
Transient transaction errors (ErrorLabel: 'TransientTransactionError') indicate temporary conditions like write conflicts or network issues. The application should catch these errors and retry the entire transaction from the beginning, as the original transaction was rolled back.
Question 13: What does the replaceOne() method do differently from updateOne()?
- It only works on arrays
- It creates a backup before replacing
- It replaces the entire document except _id rather than updating specific fields (Correct answer)
- It requires an index
Correct answer: It replaces the entire document except _id rather than updating specific fields
replaceOne() replaces the entire matching document with the new document, preserving only the _id field.
Question 14: What does the $nor operator do in a MongoDB query?
- Returns documents matching exactly one condition
- Returns documents matching all specified conditions
- Returns documents matching at least one condition
- Returns documents that fail all of the specified conditions (Correct answer)
Correct answer: Returns documents that fail all of the specified conditions
$nor returns documents that do not match any of the conditions in the array, the opposite of $or.
Question 15: What does the $count stage return in an aggregation pipeline?
- An array of all document IDs
- A single document with the count of documents passed to it (Correct answer)
- A running total of documents
- The count per group
Correct answer: A single document with the count of documents passed to it
$count outputs a single document containing a field with the count of documents that entered the stage.
Question 16: What is a chunk in MongoDB sharding?
- A contiguous range of shard key values that is stored on a single shard (Correct answer)
- A unit of replication between shards
- A batch of write operations sent to a shard
- A compressed block of documents stored on disk
Correct answer: A contiguous range of shard key values that is stored on a single shard
A chunk is a contiguous range of shard key values assigned to a specific shard. As data grows, MongoDB splits large chunks and the balancer migrates chunks between shards. The default chunk size is 128 MB in recent MongoDB versions.
Question 17: Why is it an anti-pattern to use MongoDB transactions for every write operation?
- Transactions introduce latency overhead; MongoDB's document model allows many multi-entity operations to be atomic via embedding, eliminating the need for transactions (Correct answer)
- Transactions prevent secondary reads from being consistent
- Transactions require a dedicated config server
- Transactions are not supported on standalone mongod instances
Correct answer: Transactions introduce latency overhead; MongoDB's document model allows many multi-entity operations to be atomic via embedding, eliminating the need for transactions
MongoDB's document model encourages embedding related data so that operations affecting multiple 'entities' can be done in a single atomic document write. Transactions add significant overhead (locking, oplog growth, coordination). The best practice is to model data to avoid transactions and only use them when truly necessary.
Question 18: What is a MongoDB arbiter in a replica set?
- A hidden member that is invisible to client connections
- A read-only secondary that serves analytics queries
- A member that holds no data but participates in elections to break ties (Correct answer)
- A delayed secondary used for backup purposes
Correct answer: A member that holds no data but participates in elections to break ties
An arbiter holds no data and cannot become primary. Its sole purpose is to vote in elections to break ties when an even number of data-bearing members are present. Arbiters use minimal resources but introduce a potential single point of failure.
Question 19: What is the purpose of schema versioning in MongoDB?
- To version control the database binary
- To allow gradual migration of documents to a new schema without downtime (Correct answer)
- To automatically upgrade field types on insert
- To lock the schema for production deployments
Correct answer: To allow gradual migration of documents to a new schema without downtime
The Schema Versioning Pattern adds a 'schema_version' field to documents. Application code handles multiple versions simultaneously, allowing lazy migration — old documents are updated to the new schema when they are next read or written, avoiding a big-bang migration.
Question 20: What does the $inc operator do in an update operation?
- Indexes a field
- Inserts a new field
- Includes a field in projection
- Increments or decrements a numeric field by a specified amount (Correct answer)
Correct answer: Increments or decrements a numeric field by a specified amount
$inc adds the specified value to a field's current value, and a negative value effectively decrements it.
Question 21: What is index selectivity in MongoDB performance tuning?
- How effectively an index narrows down the candidate documents (Correct answer)
- The number of fields included in a compound index
- How selectively indexes are created per collection
- The ability to select which index to use manually
Correct answer: How effectively an index narrows down the candidate documents
Index selectivity measures how much an index reduces the number of documents that need to be examined, with higher selectivity meaning fewer documents examined.
Question 22: What is the ESR rule for compound index design in MongoDB?
- Embedded, Scalar, Reference — document design patterns
- Efficient, Selective, Relevant — criteria for index creation
- Exact, Sparse, Range — use these index types together
- Equality, Sort, Range — order fields in this sequence (Correct answer)
Correct answer: Equality, Sort, Range — order fields in this sequence
The ESR rule states that in a compound index, place Equality fields first, Sort fields second, and Range fields last for optimal query performance.
Question 23: What is the recommended write concern for MongoDB transactions to ensure durability?
- { w: 1 }
- { w: 0 }
- { w: 'majority' } (Correct answer)
- { w: 'all' }
Correct answer: { w: 'majority' }
{ w: 'majority' } is the recommended write concern for transactions. It ensures the committed transaction has been written to a majority of replica set members, providing durability. Using w: 1 risks rollback if the primary fails before the secondary replicates the commit.
Question 24: What is the 'snapshot' isolation level provided by MongoDB transactions?
- Each read within the transaction sees the most recent committed data
- All reads within the transaction see a consistent snapshot of data as it existed at the transaction's start time (Correct answer)
- Reads are locked and block all writes until the transaction commits
- Reads see uncommitted data from other concurrent transactions
Correct answer: All reads within the transaction see a consistent snapshot of data as it existed at the transaction's start time
Snapshot isolation ensures all read operations within a transaction see a consistent view of the data as it existed at the logical transaction start time. This means the transaction is isolated from any committed or uncommitted changes made by concurrent transactions during its execution.
Question 25: Which operator selects documents where a field value matches any value in a specified array?
- $all
- $elemMatch
- $exists
- $in (Correct answer)
Correct answer: $in
$in matches documents where the field value is equal to any value in the specified array.
Question 26: What is a replica set in MongoDB?
- A set of identical database schema templates
- A group of mongod instances maintaining the same data set for redundancy (Correct answer)
- A set of backup files taken at the same time
- A collection of replicated index definitions
Correct answer: A group of mongod instances maintaining the same data set for redundancy
A replica set is a group of MongoDB instances that maintain the same data, providing high availability through automatic failover.
Question 27: What does the $elemMatch operator do when used in a MongoDB query on an array field?
- Counts the number of matching array elements
- Returns only the matching elements from the array
- Matches documents where at least one array element satisfies all specified conditions (Correct answer)
- Returns all elements from the matching array
Correct answer: Matches documents where at least one array element satisfies all specified conditions
$elemMatch matches documents where at least one array element meets all conditions specified within the operator.
Question 28: What type of database is MongoDB?
- Column Based
- Document Oriented (Correct answer)
- Key Value Pair
- Graph Oriented
Correct answer: Document Oriented
Explanation: <br> MongoDB, for example, is a document-oriented database that may be used on a variety of systems. According to its classification, MongoDB is a NoSQL database application that employs JSON-like documents with optional schemas.
Question 29: What does the rs.status() command display in MongoDB?
- The storage status of all replica set members
- Read/write statistics for the replica set
- The status of running query operations
- The current state, health, and replication lag of all replica set members (Correct answer)
Correct answer: The current state, health, and replication lag of all replica set members
rs.status() returns a document describing the current state of the replica set, including each member's health, state, and optime.
Question 30: Which aggregation accumulator returns the sum of numeric values in a group?
- $count
- $add
- $total
- $sum (Correct answer)
Correct answer: $sum
$sum returns the sum of all numeric values, and when used as $sum: 1, it counts the number of documents in the group.
MongoDB Associate Developer Exam
The MongoDB Associate Developer Exam validates core MongoDB development skills including CRUD operations, indexing strategies, schema design, and application integration using MongoDB drivers. It is the primary entry-level certification for developers building applications with MongoDB.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds