Indexing Questions with Answers

21 practice questions on Indexing (NoSQL & MongoDB) with answers and step-by-step solutions. Try 10 samples below, then practise all 21 free in the AptiNest app.

Sample questions

Question 1Easy

Every MongoDB collection automatically has an index on which field?

  1. _id
  2. name
  3. createdAt
  4. email

Answer: A. _id

Explanation

The _id field is always indexed and unique.

Step-by-step solution

  1. _id is required.
  2. 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?

  1. Slower reads on indexed fields
  2. Loss of data when the index is built
  3. Extra storage and slower writes
  4. Schema errors on existing documents

Answer: C. Extra storage and slower writes

Explanation

Indexes cost space and write time.

Step-by-step solution

  1. 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?

  1. db.users.index(email)
  2. db.users.createIndex('email', 'asc')
  3. db.users.addIndex({email: asc})
  4. 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

  1. Pass a key pattern object.
  2. 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?

  1. To encrypt stored data
  2. To enforce a replica count
  3. To shrink the documents
  4. To speed up queries

Answer: D. To speed up queries

Explanation

Indexes avoid scanning the entire collection.

Step-by-step solution

  1. An index is a sorted structure on one or more fields.
  2. With an index, MongoDB can find matching documents without scanning the whole collection.
  3. 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?

  1. Indexes every nth document so that the index stays small and fast
  2. Deletes null values
  3. Compresses the index
  4. 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

  1. 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?

  1. Create a separate single-field index {age: 1} and drop the city index
  2. Create a compound index {city: 1, age: 1}
  3. Replace the index with a hashed index on city so ordering is faster
  4. 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

  1. Equality field first.
  2. Sort field second.
  3. 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?

  1. Hashed index
  2. Text index
  3. 2dsphere index
  4. TTL index

Answer: B. Text index

Explanation

A text index tokenises strings for $text search.

Step-by-step solution

  1. Searching words means text search.
  2. 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?

  1. Hashed
  2. 2dsphere
  3. Text
  4. TTL

Answer: B. 2dsphere

Explanation

Geospatial queries like $near use 2d or 2dsphere indexes.

Step-by-step solution

  1. 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?

  1. find({b: 5}) only
  2. find({c: 5})
  3. find({a: 5})
  4. 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

  1. The compound index {a: 1, b: 1} is sorted by a first, then by b.
  2. MongoDB can use a leading prefix of the index keys, starting from a.
  3. 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:

  1. Is unique
  2. Is a date
  3. Is nested one level
  4. Holds an array

Answer: D. Holds an array

Explanation

MongoDB creates an index entry per array element.

Step-by-step solution

  1. Field contains array.
  2. 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 →

Related topics

See all NoSQL & MongoDB topics · All topics