MCQs on Indexes and Query Optimization | MongoDB MCQs

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

  1. 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
  2. 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
  3. 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
  4. Multi-key indexes are used for:
    a) Single numeric fields
    b) Textual fields only
    c) Fields containing arrays
    d) Geo-spatial queries
  5. Which index is best for searching phrases or keywords?
    a) Compound Index
    b) Text Index
    c) Single-field Index
    d) Multi-key Index
  6. 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

  1. 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})
  2. 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
  3. 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
  4. 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")
  5. 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
  6. 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

  1. 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
  2. The executionStats mode of explain() provides:
    a) Index creation details
    b) Detailed execution statistics
    c) Data modification history
    d) Query schema information
  3. 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
  4. 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
  5. Which operator forces MongoDB to use a specific index?
    a) $force
    b) $indexHint
    c) $useIndex
    d) $hint
  6. 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
  7. 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
  8. 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

QnoAnswer (Option with Text)
1c) Text Index
2b) Index on multiple fields in a defined order
3b) Single-field Index
4c) Fields containing arrays
5b) Text Index
6b) Dynamic Index
7b) db.collection.createIndex({name: 1})
8b) Blocks duplicate values
9b) Delete documents after a specified time
10c) db.collection.dropIndex("age_1")
11c) Allows other operations during index creation
12b) db.collection.listIndexes()
13b) To analyze query execution plans
14b) Detailed execution statistics
15a) A query that uses an index to fetch all required data
16c) Use explain()
17d) $hint
18b) Uniqueness of values in an indexed field
19b) Increased storage overhead
20d) Schema design

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top