Sample questions
Question 1Easy
Every MongoDB collection automatically has an index on which field?
- _id
- name
- createdAt
- email
Answer: A. _id
Explanation
The _id field is always indexed and unique.
Step-by-step solution
- _id is required.
- It gets a unique index.
Why the other options are wrong
- B. Only _id gets an automatic index; name does not.
- C. createdAt is not indexed automatically in every collection.
- D. email is not indexed automatically; only _id is.
Question 2Easy
Which of these is a likely downside of adding an index?
- Slower reads on indexed fields
- Loss of data when the index is built
- Extra storage and slower writes
- Schema errors on existing documents
Answer: C. Extra storage and slower writes
Explanation
Indexes cost space and write time.
Step-by-step solution
- Trade-off.
Why the other options are wrong
- A. Indexes speed up reads, so they do not slow reads on indexed fields.
- B. Building an index does not lose data in existing documents.
- D. Adding an index does not cause schema errors on documents.
Question 3Easy
Which command creates an ascending index on the field email?
- db.users.index(email)
- db.users.createIndex('email', 'asc')
- db.users.addIndex({email: asc})
- db.users.createIndex({email: 1})
Answer: D. db.users.createIndex({email: 1})
Explanation
createIndex takes a key pattern: 1 for ascending, -1 for descending.
Step-by-step solution
- Pass a key pattern object.
- Use 1.
Why the other options are wrong
- A. There is no db.users.index method; use createIndex.
- B. createIndex takes a key object with 1 for ascending, not 'asc'.
- C. addIndex is not a MongoDB shell method for creating indexes.
Question 4Easy
What is the main purpose of an index in MongoDB?
- To encrypt stored data
- To enforce a replica count
- To shrink the documents
- To speed up queries
Answer: D. To speed up queries
Explanation
Indexes avoid scanning the entire collection.
Step-by-step solution
- An index is a sorted structure on one or more fields.
- With an index, MongoDB can find matching documents without scanning the whole collection.
- So the main purpose is to speed up queries; the correct answer is option D.
Why the other options are wrong
- A. Encryption is a separate feature, not the job of an index.
- B. The replica count is set by the replica set, not by an index.
- C. An index does not change the size of documents.
Tip: Index = faster reads; it costs some extra space and slower writes.
Question 5Medium
What does a sparse index do?
- Indexes every nth document so that the index stays small and fast
- Deletes null values
- Compresses the index
- Indexes only documents that contain the indexed field
Answer: D. Indexes only documents that contain the indexed field
Explanation
Documents lacking the field are left out of a sparse index.
Step-by-step solution
- Sparse = skip those without field.
Why the other options are wrong
- A. A sparse index does not sample every nth document.
- B. A sparse index does not delete nulls; it skips missing fields.
- C. Compression is separate.
Question 6Medium
You run find({city:'Pune'}).sort({age:1}) and explain shows a SORT stage in memory after IXSCAN on {city:1}. How can you remove the in-memory sort?
- Create a separate single-field index {age: 1} and drop the city index
- Create a compound index {city: 1, age: 1}
- Replace the index with a hashed index on city so ordering is faster
- Add a TTL option so the sort is cached automatically
Answer: B. Create a compound index {city: 1, age: 1}
Explanation
A compound index with the equality field first and then the sort field returns documents already sorted.
Step-by-step solution
- Equality field first.
- Sort field second.
- The index gives pre-sorted results.
Why the other options are wrong
- A. The city filter would lose its index.
- C. Hashed indexes carry no ordering.
- D. TTL only removes expired documents.
Question 7Medium
Which index type supports searching words in string fields?
- Hashed index
- Text index
- 2dsphere index
- TTL index
Answer: B. Text index
Explanation
A text index tokenises strings for $text search.
Step-by-step solution
- Searching words means text search.
- Create a text index.
Why the other options are wrong
- A. Hashed supports equality and hash sharding.
- C. 2dsphere is for geospatial.
- D. TTL expires documents.
Question 8Medium
Which index would best serve find({loc: {$near: ...}}) for coordinates?
- Hashed
- 2dsphere
- Text
- TTL
Answer: B. 2dsphere
Explanation
Geospatial queries like $near use 2d or 2dsphere indexes.
Step-by-step solution
- Location data uses geospatial index.
Why the other options are wrong
- A. Hashed is for equality.
- C. Text indexes support word search, not proximity on coordinates.
- D. TTL indexes expire documents by date; they do not support geo queries.
Question 9Hard
Given a compound index {a: 1, b: 1}, which query can use it efficiently?
- find({b: 5}) only
- find({c: 5})
- find({a: 5})
- None; compound indexes need all fields
Answer: C. find({a: 5})
Explanation
A compound index supports queries on its prefix; a alone is a prefix, b alone is not.
Step-by-step solution
- The compound index {a: 1, b: 1} is sorted by a first, then by b.
- MongoDB can use a leading prefix of the index keys, starting from a.
- find({a: 5}) uses the prefix a, so it is efficient; the correct answer is option C.
Why the other options are wrong
- A. A filter only on b skips the leading key a, so this index is not used efficiently.
- B. Field c is not in the index at all.
- D. A compound index works on a prefix of its keys, so not all fields are needed.
Tip: A compound index works left to right: the query must use the first key.
Question 10Hard
A multikey index is created automatically when the indexed field:
- Is unique
- Is a date
- Is nested one level
- Holds an array
Answer: D. Holds an array
Explanation
MongoDB creates an index entry per array element.
Step-by-step solution
- Field contains array.
- One key per element.
Why the other options are wrong
- A. Uniqueness does not make a field produce a multikey index.
- B. A date field does not create a multikey index.
- C. Nesting alone is not multikey.
Practise all 21 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →