MCQs Of Day 9: Indexes in MongoDB
1. What is an index in MongoDB?
a) A way to store documents
b) A data structure that improves query performance
c) A type of collection
d) A method to insert documents into a collection
Answer: b) A data structure that improves query performance
Explanation: Indexes help MongoDB perform queries more efficiently by allowing faster searches without scanning the entire collection.
2. Which of the following is the default index created in every MongoDB collection?
a) Compound Index
b) Single Field Index
c) Geospatial Index
d) _id Index
Answer: d) _id Index
Explanation: MongoDB automatically creates an index on the _id field, which is unique for every document in the collection.
3. What is the purpose of creating an index in MongoDB?
a) To speed up data insertion
b) To reduce the storage size of the database
c) To improve the speed of data retrieval operations
d) To make the database schema dynamic
Answer: c) To improve the speed of data retrieval operations
Explanation: Indexes optimize the querying process by allowing faster data retrieval based on the indexed fields.
4. How do you create an index on the name field in MongoDB?
a) db.collection.createIndex({name: -1});
b) db.collection.createIndex({name: 1});
c) db.collection.createIndex(name);
d) db.collection.createIndex(name: 1);
Answer: b) db.collection.createIndex({name: 1});
Explanation: The syntax db.collection.createIndex({field: 1}) is used to create an ascending index on the name field.
5. What does the number 1 represent when creating an index in MongoDB?
a) Ascending order
b) Descending order
c) Default order
d) Multikey index
Answer: a) Ascending order
Explanation: 1 indicates that the index should be sorted in ascending order, while -1 indicates descending order.
6. Which of the following is a compound index in MongoDB?
a) db.collection.createIndex({field1: 1, field2: -1});
b) db.collection.createIndex({field1: 1});
c) db.collection.createIndex({field2: -1});
d) db.collection.createIndex({field1: "hashed"});
Answer: a) db.collection.createIndex({field1: 1, field2: -1});
Explanation: A compound index is created on multiple fields (in this case field1 and field2).
7. What is a multikey index in MongoDB?
a) An index on a single field with multiple values
b) An index on array fields
c) An index created on multiple collections
d) An index on object fields
Answer: b) An index on array fields
Explanation: MongoDB creates a multikey index when a field contains an array. This allows efficient querying on array elements.
8. How can you view all the indexes of a collection in MongoDB?
a) db.collection.getIndexes();
b) db.collection.viewIndexes();
c) db.collection.showIndexes();
d) db.collection.indexList();
Answer: a) db.collection.getIndexes();
Explanation: The getIndexes() method returns a list of all indexes on a collection.
9. Which index type is most useful for geospatial queries in MongoDB?
a) Compound Index
b) Multikey Index
c) Text Index
d) Geospatial Index
Answer: d) Geospatial Index
Explanation: Geospatial indexes are specifically designed for queries that involve geographic coordinates.
10. How do you drop an index by name in MongoDB?
a) db.collection.dropIndex("index_name");
b) db.collection.removeIndex("index_name");
c) db.collection.deleteIndex("index_name");
d) db.collection.dropIndex({index_name: 1});
Answer: a) db.collection.dropIndex("index_name");
Explanation: The dropIndex() method is used to remove a specific index by its name.
11. What happens when you create an index on a field that is frequently updated?
a) It improves performance
b) It reduces storage space
c) It can slow down write operations
d) It has no impact on write operations
Answer: c) It can slow down write operations
Explanation: Indexes can slow down insert, update, and delete operations because MongoDB must update the index whenever the indexed field changes.
12. What is the best practice for indexing in MongoDB?
a) Index all fields in the collection
b) Index only frequently queried fields
c) Create indexes on every field that has a unique value
d) Create compound indexes on every query field
Answer: b) Index only frequently queried fields
Explanation: Indexing unnecessary fields can slow down the database and increase storage usage, so it's best to index fields that are frequently used in queries.
13. Which index type should be used for full-text search in MongoDB?
a) Geospatial Index
b) Text Index
c) Compound Index
d) Single Field Index
Answer: b) Text Index
Explanation: A text index is used for performing full-text searches on string content in MongoDB.
14. What does the hashed index type in MongoDB do?
a) Hashes the value of the indexed field
b) Creates a compound index
c) Sorts the indexed field in descending order
d) Is used for text search
Answer: a) Hashes the value of the indexed field
Explanation: A hashed index is used primarily for sharding, as it evenly distributes documents across the cluster based on a hash of the field's value.
15. How does a compound index affect query performance in MongoDB?
a) It slows down queries that filter on the indexed fields
b) It speeds up queries that filter on multiple indexed fields
c) It increases memory usage without improving query speed
d) It reduces the storage size of documents
Answer: b) It speeds up queries that filter on multiple indexed fields
Explanation: Compound indexes improve performance for queries that involve multiple fields by indexing them together.
16. When would you use a hashed index in MongoDB?
a) When the field has a small number of unique values
b) When you want to perform range queries
c) When using sharding to distribute data across clusters
d) When performing full-text search
Answer: c) When using sharding to distribute data across clusters
Explanation: Hashed indexes are particularly useful for sharding because they help distribute documents evenly across the cluster.
17. Which of the following is NOT a valid MongoDB index type?
a) Geospatial Index
b) Text Index
c) Geographical Index
d) Compound Index
Answer: c) Geographical Index
Explanation: There is no specific "Geographical Index" in MongoDB; the correct term is Geospatial Index.
18. Which of the following is true about MongoDB indexes?
a) Indexes always reduce storage size
b) Indexes speed up query performance but can slow down write operations
c) Indexes automatically remove duplicate values
d) MongoDB does not support indexing on array fields
Answer: b) Indexes speed up query performance but can slow down write operations
Explanation: Indexes can improve query performance, but they require additional time to maintain during insert, update, or delete operations.
19. What is a typical use case for a multikey index?
a) Querying on array fields
b) Sorting documents
c) Storing documents in multiple collections
d) Creating compound indexes on multiple fields
Answer: a) Querying on array fields
Explanation: Multikey indexes are automatically created by MongoDB when a field contains an array, allowing efficient querying on array elements.
20. Which method in MongoDB creates an index on a collection?
a) db.collection.createIndex()
b) db.createIndex()
c) db.index.create()
d) db.collection.createIndexes()
Answer: a) db.collection.createIndex()
Explanation: The createIndex() method is used to create indexes on fields in a MongoDB collection.
21. What is the result of dropping all indexes in MongoDB except for the default _id index?
a) MongoDB will automatically recreate all dropped indexes
b) It will remove all indexes, slowing down query performance
c) The database will throw an error
d) It has no effect on query performance
Answer: b) It will remove all indexes, slowing down query performance
Explanation: Dropping indexes, especially compound or frequently used indexes, can negatively impact query performance.
22. How do compound indexes improve query performance in MongoDB?
a) By reducing the storage requirement for the collection
b) By allowing queries to use multiple indexed fields efficiently
c) By creating indexes for every query
d) By indexing only the most frequently accessed documents
Answer: b) By allowing queries to use multiple indexed fields efficiently
Explanation: Compound indexes improve the performance of queries that involve multiple fields by indexing them together.
23. How can you create an index on multiple fields in MongoDB?
a) By creating separate indexes on each field
b) By creating a single compound index on multiple fields
c) By creating a multikey index
d) By using createIndexes() method
Answer: b) By creating a single compound index on multiple fields
Explanation: A compound index is created when multiple fields are indexed together, improving query performance for queries that involve those fields.
24. What is the effect of an index on write operations in MongoDB?
a) Indexes have no effect on write operations
b) Write operations are faster with indexes
c) Write operations can be slower because indexes need to be updated
d) Write operations are completely blocked when indexes are used
Answer: c) Write operations can be slower because indexes need to be updated
Explanation: When a document is inserted, updated, or deleted, MongoDB has to update the relevant indexes, which can slow down write operations.
25. Which MongoDB command lists all the indexes in a collection?
a) db.collection.getIndexes()
b) db.collection.listIndexes()
c) db.collection.showIndexes()
d) db.collection.indexList()
Answer: a) db.collection.getIndexes()
Explanation: The getIndexes() method is used to retrieve a list of all indexes on a collection.
26. What happens if an index is created on a frequently updated field?
a) The index will automatically update itself without performance overhead
b) It will reduce the query performance
c) It can cause slower write operations due to index updates
d) It will automatically delete any duplicate documents
Answer: c) It can cause slower write operations due to index updates
Explanation: Indexes must be updated whenever the indexed field changes, which can slow down write operations.
27. What is the function of a hashed index in MongoDB?
a) It sorts data in descending order
b) It helps perform range queries
c) It evenly distributes documents across a sharded cluster
d) It improves text search capabilities
Answer: c) It evenly distributes documents across a sharded cluster
Explanation: Hashed indexes are primarily used in sharded clusters to ensure that documents are distributed evenly across different shards based on the hash value of the indexed field.
28. What is a geospatial index used for in MongoDB?
a) To perform geographic location-based queries
b) To speed up text search queries
c) To create sorted indexes for numerical values
d) To generate random data for testing
Answer: a) To perform geographic location-based queries
Explanation: Geospatial indexes are used for querying geographical data and performing location-based searches.
29. Which of the following is true about the _id index in MongoDB?
a) It can be removed from a collection
b) It is automatically created for each collection
c) It is created only if the user specifies it
d) It is not necessary for document retrieval
Answer: b) It is automatically created for each collection
Explanation: MongoDB automatically creates a unique _id index for every collection.
30. How do you create an index on an array field in MongoDB?
a) Create a compound index
b) Use a multikey index
c) Use a hashed index
d) Use a text index
Answer: b) Use a multikey index
Explanation: MongoDB automatically creates a multikey index when an array field is indexed, allowing efficient queries on array elements.
31. Can a compound index have both ascending and descending fields?
a) No, all fields in a compound index must be the same direction
b) Yes, it can have both ascending and descending fields
c) No, compound indexes are not allowed in MongoDB
d) Yes, but it will not improve performance
Answer: b) Yes, it can have both ascending and descending fields
Explanation: MongoDB allows you to create compound indexes with fields sorted in both ascending and descending orders.
32. When should you use a text index in MongoDB?
a) For indexing numeric fields
b) For full-text search capabilities
c) For geospatial data
d) For creating compound indexes
Answer: b) For full-text search capabilities
Explanation: A text index is specifically used for full-text search on string fields in MongoDB.
33. Which of the following MongoDB operations can benefit the most from indexing?
a) Insert
b) Update
c) Query
d) Delete
Answer: c) Query
Explanation: Indexing improves the performance of queries by reducing the need to scan the entire collection for matching documents.
34. What is the primary drawback of creating too many indexes in MongoDB?
a) Increased storage usage
b) Faster write operations
c) Reduced query performance
d) Increased document size
Answer: a) Increased storage usage
Explanation: Each index takes up additional storage space, so creating too many indexes can increase the overall storage requirements of the database.
35. Which type of index is best for range queries in MongoDB?
a) Text Index
b) Compound Index
c) Hashed Index
d) Single Field Index
Answer: b) Compound Index
Explanation: Compound indexes are best suited for range queries, especially when multiple fields are involved.
36. What does an ascending index do in MongoDB?
a) Sorts the indexed field in descending order
b) Sorts the indexed field in ascending order
c) Sorts documents randomly
d) Creates a full-text search index
Answer: b) Sorts the indexed field in ascending order
Explanation: An ascending index sorts documents based on the indexed field in increasing order.
37. What is the main purpose of using an index in MongoDB?
a) To make data retrieval slower
b) To optimize query performance
c) To reduce the size of the database
d) To store duplicate documents
Answer: b) To optimize query performance
Explanation: The primary purpose of an index is to speed up query processing by allowing faster searches and retrievals.
38. Can indexes be used for sorting query results in MongoDB?
a) Yes, indexes are automatically used for sorting
b) No, indexes only speed up filtering, not sorting
c) Yes, but only if you use the $sort operator
d) No, sorting can only be done manually
Answer: a) Yes, indexes are automatically used for sorting
Explanation: MongoDB can use indexes to efficiently sort query results, especially if the indexed field is used for sorting.
39. How do you create a hashed index in MongoDB?
a) db.collection.createIndex({field: "hashed"});
b) db.collection.createIndex({field: 1});
c) db.collection.createIndex({field: -1});
d) db.collection.createIndex({field: "text"});
Answer: a) db.collection.createIndex({field: "hashed"});
Explanation: The "hashed" index type is specified for fields that need to be evenly distributed, such as for sharding purposes.
40. When should you drop an index in MongoDB?
a) When the field indexed is rarely queried
b) When the collection is large
c) After every query
d) When the database is shut down
Answer: a) When the field indexed is rarely queried
Explanation: If an index is rarely used in queries, it should be dropped to save storage and improve write performance.
41. How does a hashed index improve query performance in a sharded MongoDB cluster?
a) By speeding up all queries
b) By evenly distributing documents across multiple shards
c) By reducing document size
d) By removing duplicate documents
Answer: b) By evenly distributing documents across multiple shards
Explanation: Hashed indexes ensure that documents are distributed evenly across shards, improving performance in a sharded cluster.
42. Which MongoDB command removes all indexes except the default _id index?
a) db.collection.dropIndexes();
b) db.collection.removeIndexes();
c) db.collection.deleteIndexes();
d) db.collection.dropAllIndexes();
Answer: a) db.collection.dropIndexes();
Explanation: The dropIndexes() method removes all custom indexes, leaving only the default _id index.
43. Which of the following MongoDB index types allows you to perform efficient text search?
a) Geospatial Index
b) Text Index
c) Compound Index
d) Multikey Index
Answer: b) Text Index
Explanation: Text indexes allow efficient full-text searches on string fields in MongoDB.
44. How can you ensure that a geospatial query works efficiently in MongoDB?
a) By using a compound index
b) By creating a geospatial index
c) By indexing the entire document
d) By using a hashed index
Answer: b) By creating a geospatial index
Explanation: Geospatial indexes are designed to optimize location-based queries in MongoDB.
45. Which of the following is an advantage of using a compound index?
a) It supports sorting only
b) It improves performance for queries involving multiple fields
c) It can only be used for single-field queries
d) It requires less storage space than single-field indexes
Answer: b) It improves performance for queries involving multiple fields
Explanation: Compound indexes are designed to optimize queries that involve more than one field.
46. What is the purpose of a multikey index in MongoDB?
a) To index fields that contain arrays
b) To improve performance for range queries
c) To index multiple collections simultaneously
d) To store multiple values for a single field
Answer: a) To index fields that contain arrays
Explanation: Multikey indexes are automatically created by MongoDB when a field contains an array.
47. What is the result of creating an index on a frequently updated field in MongoDB?
a) It significantly reduces the overall performance
b) It speeds up all operations
c) It can slow down write operations
d) It does not affect performance
Answer: c) It can slow down write operations
Explanation: Indexes need to be updated whenever the indexed field changes, which can slow down write operations.
48. What kind of index is needed for queries that match exact text patterns in MongoDB?
a) Geospatial Index
b) Hashed Index
c) Text Index
d) Compound Index
Answer: c) Text Index
Explanation: Text indexes are specifically designed to support text search queries, including exact matches and partial text matching.
49. Which of the following is true about MongoDB indexes?
a) They are only useful for single-field queries
b) They speed up query performance but can slow down data insertion
c) They increase the document size
d) They are automatically created for all fields
Answer: b) They speed up query performance but can slow down data insertion
Explanation: Indexes improve query performance but add overhead during write operations as they need to be updated.
50. What is the primary advantage of creating indexes on frequently queried fields in MongoDB?
a) Reduced query performance
b) Increased write operations
c) Improved query performance
d) Increased storage requirements
Answer: c) Improved query performance
Explanation: Indexes help MongoDB retrieve documents more efficiently, improving query performance, especially for complex or frequent queries.
