MongoDB MongoDB Indexing and Performance 2 — Questions and Answers
Question 1: What is a covered query in MongoDB?
- A query protected by access control
- A query that can be satisfied entirely using an index without accessing documents (Correct answer)
- A query that covers all documents in a collection
- A query with full-text search coverage
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 2: Which type of index supports efficient queries on fields within embedded arrays?
- Sparse index
- Text index
- Multikey index (Correct answer)
- Hashed index
Correct answer: Multikey index
MongoDB automatically creates a multikey index when an indexed field contains array values, creating index entries for each array element.
Question 3: What is the purpose of a TTL (Time-To-Live) index in MongoDB?
- Tracks the time it takes to traverse an index
- Automatically deletes documents after a specified time period (Correct answer)
- Limits how long index builds can run
- Sets the maximum age of index entries
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 4: What does the 'nscannedObjects' field in explain() output indicate?
- Number of indexes scanned
- Number of documents examined to fulfill the query (Correct answer)
- Number of namespace objects
- Number of nested sub-documents scanned
Correct answer: Number of documents examined to fulfill the query
nscannedObjects (or docsExamined in newer versions) shows how many actual documents were read, with lower numbers indicating better index usage.
Question 5: Which index type allows full-text search queries on string content?
- Hashed index
- Wildcard index
- Text index (Correct answer)
- Regex index
Correct answer: Text index
A text index tokenizes and stems string fields to support $text search queries, with at most one text index per collection.
Question 6: What is a partial index in MongoDB?
- An incomplete index being built in the background
- An index that only includes documents meeting a specified filter expression (Correct answer)
- An index covering only part of a field's value
- A degraded index after a hardware failure
Correct answer: An index that only includes documents meeting a specified filter expression
A partial index indexes only documents that satisfy a specified filter, resulting in smaller, more efficient indexes for common query patterns.
What is a covered query in MongoDB?