MongoDB MCQs – Indexing Basics

11.) Which MongoDB operator is optimized by a geospatial index?

A) $geoNear
B) $or
C) $match
D) $regex

Answer: Option A

Explanation: The $geoNear operator performs geospatial queries and is optimized by a geospatial index.

12.) What is the purpose of the unique option when creating an index?

A) To make the index case-sensitive
B) To enforce unique values for the indexed field
C) To allow duplicate values in the index
D) To improve sorting performance

Answer: Option B

Explanation: The unique option ensures that no two documents in the collection have the same value for the indexed field.

13.) How do you create a unique index in MongoDB?

A) db.collection.createIndex({ field: 1 }, { unique: true })
B) db.collection.addUniqueIndex({ field: 1 })
C) db.collection.createUniqueIndex({ field: 1 })
D) db.collection.index({ field: 1, unique: true })

Answer: Option A

Explanation: The createIndex() method with { unique: true } enforces uniqueness for the indexed field.

14.) Which of the following is NOT true about indexes?

A) They improve query performance.
B) They take up additional storage space.
C) They slow down write operations.
D) They are always created automatically for all fields.

Answer: Option D

Explanation: MongoDB automatically creates an index only on the _id field. Other indexes must be created explicitly.

15.) Which type of geospatial index does MongoDB support?

A) 2D index
B) 3D index
C) 2D sphere index
D) Both A and C

Answer: Option D

Explanation: MongoDB supports 2D and 2D sphere indexes for geospatial data.

16.) What is the default collation for indexes in MongoDB?

A) Case-sensitive
B) Case-insensitive
C) Language-specific
D) Binary

Answer: Option D

Explanation: MongoDB’s default collation for indexes is binary, which is case-sensitive and based on binary comparison.

17.) Which of the following methods is used to drop all indexes except _id?

A) db.collection.dropIndexes()
B) db.collection.dropIndex(“*”)
C) db.collection.deleteIndexes()
D) db.collection.removeIndexes()

Answer: Option A

Explanation: The dropIndexes() method removes all indexes except the default _id index.

18.) Which type of index is suitable for searching fields in natural language?

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

Answer: Option A

Explanation: Text indexes are designed for full-text search, enabling efficient natural language searches.

19.) What happens if you try to insert a duplicate value into a field with a unique index?

A) The duplicate value is ignored.
B) The operation fails with an error.
C) The document is inserted without the duplicate value.
D) A new unique index is created.

Answer: Option B

Explanation: A unique index enforces uniqueness, so inserting a duplicate value results in an error.

20.) How do indexes affect write performance in MongoDB?

A) They improve write performance.
B) They have no impact on write performance.
C) They slow down write performance slightly.
D) They prevent write operations.

Answer: Option C

Explanation: Indexes add overhead to write operations because they need to be updated whenever data is written to the collection.

Leave a Reply

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