Indexes and query optimization are essential for enhancing the efficiency of MongoDB. Learn about various types of indexes, how to create and manage them, and the role of query execution in improving database performance. Boost your MongoDB skills with these multiple-choice questions that cover key concepts step by step.
Multiple-Choice Questions (MCQs)
1. Types of Indexes
Which type of index is used to support text search in MongoDB? a) Single-field Index b) Compound Index c) Text Index d) Multi-key Index
What is a compound index? a) Index on a single field b) Index on multiple fields in a defined order c) Index on array fields d) Index specifically for sorting
What type of index is automatically created on the _id field in every MongoDB collection? a) Text Index b) Single-field Index c) Compound Index d) Multi-key Index
Multi-key indexes are used for: a) Single numeric fields b) Textual fields only c) Fields containing arrays d) Geo-spatial queries
Which index is best for searching phrases or keywords? a) Compound Index b) Text Index c) Single-field Index d) Multi-key Index
Which of the following is not a valid index type in MongoDB? a) Sparse Index b) Dynamic Index c) Hashed Index d) TTL Index
2. Creating and Managing Indexes
The command to create an index on the field name is: a) db.createIndex({name: 1}) b) db.collection.createIndex({name: 1}) c) db.collection.ensureIndex({name: 1}) d) db.addIndex({name: 1})
What does the option {unique: true} ensure while creating an index? a) Allows duplicate values b) Blocks duplicate values c) Sorts data automatically d) Enables TTL functionality
The TTL index is used to: a) Index arrays b) Delete documents after a specified time c) Perform geo-spatial queries d) Improve text search
To drop an index named age_1, the command is: a) db.collection.dropIndex({age: 1}) b) db.collection.removeIndex({age: 1}) c) db.collection.dropIndex("age_1") d) db.collection.deleteIndex("age_1")
What does the {background: true} option do when creating an index? a) Makes the index faster b) Creates the index in the foreground c) Allows other operations during index creation d) Avoids locking the database
How do you list all indexes of a collection? a) db.collection.getIndexInfo() b) db.collection.listIndexes() c) db.collection.indexDetails() d) db.collection.findIndexes()
3. Query Execution and Performance Insights
What is the purpose of the explain() method in MongoDB? a) To show the document schema b) To analyze query execution plans c) To create indexes d) To validate data types
The executionStats mode of explain() provides: a) Index creation details b) Detailed execution statistics c) Data modification history d) Query schema information
A covered query is: a) A query that uses an index to fetch all required data b) A query that scans the entire collection c) A query with no sorting involved d) A query limited to text fields
How can you check if a query is using an index? a) Use showIndexes() b) Use db.collection.getIndexes() c) Use explain() d) Check document count
Which operator forces MongoDB to use a specific index? a) $force b) $indexHint c) $useIndex d) $hint
What does the term “index cardinality” refer to? a) Index storage size b) Uniqueness of values in an indexed field c) Number of indexes in a collection d) Number of documents returned by the query
What is the main performance drawback of creating too many indexes? a) Reduced query speed b) Increased storage overhead c) Complex query structure d) Loss of indexed data
Which of the following impacts MongoDB query performance the most? a) Index size b) Query filter type c) Use of explain() d) Schema design
Answer Key
Qno
Answer (Option with Text)
1
c) Text Index
2
b) Index on multiple fields in a defined order
3
b) Single-field Index
4
c) Fields containing arrays
5
b) Text Index
6
b) Dynamic Index
7
b) db.collection.createIndex({name: 1})
8
b) Blocks duplicate values
9
b) Delete documents after a specified time
10
c) db.collection.dropIndex("age_1")
11
c) Allows other operations during index creation
12
b) db.collection.listIndexes()
13
b) To analyze query execution plans
14
b) Detailed execution statistics
15
a) A query that uses an index to fetch all required data