MongoDB MCQs – Indexing Basics

Indexes are a critical aspect of MongoDB for optimizing query performance and ensuring efficient data retrieval. By reducing the amount of data scanned during queries, indexes can significantly improve application speed. These MCQs help candidates grasp the key concepts and practical applications of indexing, preparing them for interview questions on database optimization and design.

1.) What is the purpose of an index in MongoDB?

A) To reduce storage size
B) To improve query performance
C) To enforce schema validation
D) To create relationships between documents

Answer: Option B

Explanation: Indexes in MongoDB improve the speed of query execution by reducing the amount of data the database needs to scan.

2.) What type of index is automatically created for every MongoDB collection?

A) Text Index
B) Geospatial Index
C) Single Field Index on _id
D) Compound Index

Answer: Option C

Explanation: MongoDB automatically creates a single-field index on the _id field for every collection, ensuring quick lookups by _id.

3.) How do you create a single field index in MongoDB?

A) db.collection.addIndex({ field: 1 })
B) db.collection.createIndex({ field: 1 })
C) db.collection.index({ field: 1 })
D) db.collection.addSingleIndex({ field: 1 })

Answer: Option B

Explanation: The createIndex() method is used to create indexes in MongoDB. { field: 1 } creates an ascending index on the specified field.

4.) What does { field: -1 } specify when creating an index?

A) No index
B) Descending index
C) Text index
D) Geospatial index

Answer: Option B

Explanation: { field: -1 } creates a descending index, which can optimize queries that sort data in descending order.

5.) What is a compound index?

A) An index that combines multiple fields
B) An index created on a single field
C) A special type of text index
D) A geospatial index

Answer: Option A

Explanation: A compound index is created on multiple fields, allowing queries to efficiently filter and sort by multiple criteria.

6.) Which method removes an index from a collection?

A) db.collection.removeIndex({ field: 1 })
B) db.collection.deleteIndex({ field: 1 })
C) db.collection.dropIndex({ field: 1 })
D) db.collection.clearIndex({ field: 1 })

Answer: Option C

Explanation: The dropIndex() method is used to remove an index from a collection.

7.) Which type of index is used to support text search in MongoDB?

A) Geospatial Index
B) Compound Index
C) Text Index
D) Full-text Index

Answer: Option C

Explanation: A text index supports text searches in MongoDB, enabling efficient searching within string content.

8.) What is the command to create a text index in MongoDB?

A) db.collection.createTextIndex({ field: 1 })
B) db.collection.createIndex({ field: “text” })
C) db.collection.addTextIndex({ field: 1 })
D) db.collection.indexText({ field: 1 })

Answer: Option B

Explanation: To create a text index, use createIndex() with { field: “text” } as the parameter.

9.) What does the db.collection.getIndexes() command do?

A) Displays the query plan
B) Deletes all indexes
C) Lists all indexes on the collection
D) Creates a default index

Answer: Option C

Explanation: The getIndexes() method displays all existing indexes on a MongoDB collection.

10.) Which of the following is true about a compound index?

A) The order of fields in a compound index does not matter.
B) The order of fields in a compound index matters.
C) Compound indexes are automatically created on all fields.
D) Compound indexes cannot include more than two fields.

Answer: Option B

Explanation: The order of fields in a compound index is crucial as it determines how the index is used in queries.

Leave a Reply

Your email address will not be published. Required fields are marked *