MongoDB Multikey Index Practice Questions with Solutions

Introduction

A multikey index in MongoDB is an index created on a field that contains an array. MongoDB automatically makes the index multikey when the indexed field contains array values. This is useful for searching documents based on array elements such as skills, tags, subjects, or embedded documents inside arrays. In this chapter, you will practice creating multikey indexes, querying arrays, using compound indexes with arrays, checking query plans, and removing indexes. MongoDB Multikey Index practice questions with solutions to help you understand the concepts.

Q1. Create a Multikey Index on an Array Field

Problem Statement

The students collection contains a skills array. Create an index on the skills field.

MongoDB Command / Query

db.students.createIndex({
  skills: 1
})

Expected Output

skills_1

If skills contains arrays such as:

{
  name: "Rahul",
  skills: ["Python", "SQL", "MongoDB"]
}

MongoDB automatically treats the index as a multikey index.

You do not need to specify a special multikey: true option.


Q2. Find Students with a Specific Skill

Problem Statement

Find all students whose skills array contains Python.

MongoDB Command / Query

db.students.find({
  skills: "Python"
})

Expected Output

{
  name: "Rahul",
  skills: ["Python", "SQL", "MongoDB"]
}
{
  name: "Neha",
  skills: ["Python", "HTML", "CSS"]
}

Because skills is an array, MongoDB can match the value Python against its elements.

The multikey index on skills can support this type of query.


Q3. Find Students Having Any of Multiple Skills

Problem Statement

Find students whose skills array contains either Python or JavaScript.

MongoDB Command / Query

db.students.find({
  skills: {
    $in: ["Python", "JavaScript"]
  }
})

Expected Output

{
  name: "Rahul",
  skills: ["Python", "SQL", "MongoDB"]
}
{
  name: "Aman",
  skills: ["JavaScript", "HTML", "CSS"]
}

$in matches a document when at least one array element matches a value in the specified list.


Q4. Find Students Having All Required Skills

Problem Statement

Find students who have both Python and SQL in their skills array.

MongoDB Command / Query

db.students.find({
  skills: {
    $all: ["Python", "SQL"]
  }
})

Expected Output

{
  name: "Rahul",
  skills: ["Python", "SQL", "MongoDB"]
}

$all requires all specified values to be present in the array.

The multikey index on skills can be relevant to array queries, although the exact execution plan depends on the query and available indexes.


Q5. Create a Multikey Index on an Array of Embedded Documents

Problem Statement

The students collection contains a results array containing embedded documents. Create an index on results.subject.

MongoDB Command / Query

db.students.createIndex({
  "results.subject": 1
})

Expected Output

results.subject_1

For example, a document may look like:

{
  name: "Priya",
  results: [
    {
      subject: "Python",
      marks: 85
    },
    {
      subject: "SQL",
      marks: 78
    }
  ]
}

Because results is an array, the index on results.subject becomes a multikey index.


Q6. Search an Array of Embedded Documents

Problem Statement

Find students who have a result for the Python subject.

MongoDB Command / Query

db.students.find({
  "results.subject": "Python"
})

Expected Output

{
  name: "Priya",
  results: [
    {
      subject: "Python",
      marks: 85
    },
    {
      subject: "SQL",
      marks: 78
    }
  ]
}

The index on results.subject can help MongoDB efficiently search the indexed array field.


Q7. Use $elemMatch with an Indexed Array

Problem Statement

Find students who have a Python result with marks greater than 80.

MongoDB Command / Query

db.students.find({
  results: {
    $elemMatch: {
      subject: "Python",
      marks: { $gt: 80 }
    }
  }
})

Expected Output

{
  name: "Priya",
  results: [
    {
      subject: "Python",
      marks: 85
    },
    {
      subject: "SQL",
      marks: 78
    }
  ]
}

$elemMatch ensures that the subject and marks conditions apply to the same array element.

This is especially important when querying arrays of embedded documents.


Q8. Check Whether a Multikey Index Is Used

Problem Statement

Use explain() to inspect a query searching for students who have the MongoDB skill.

MongoDB Command / Query

db.students.find({
  skills: "MongoDB"
}).explain("executionStats")

Expected Output

The winning execution plan may contain:

{
  stage: "IXSCAN"
}

The execution statistics can also show information indicating that the selected index is:

skills_1

IXSCAN means MongoDB is scanning an index.

The exact execution plan depends on the collection’s data, available indexes, and query planner.


Q9. Create a Compound Multikey Index

Problem Statement

Create an index on course and the skills array.

MongoDB Command / Query

db.students.createIndex({
  course: 1,
  skills: 1
})

Expected Output

course_1_skills_1

If skills is an array, this compound index becomes a multikey index.

For example:

{
  name: "Rahul",
  course: "Python",
  skills: ["Python", "SQL", "MongoDB"]
}

The index can support queries involving the compound index fields, subject to MongoDB’s multikey index rules and query planner.


Q10. Remove a Multikey Index

Problem Statement

Remove the skills_1 index from the students collection.

MongoDB Command / Query

db.students.dropIndex("skills_1")

Expected Output

{
  nIndexesWas: 2,
  ok: 1
}

The exact value of nIndexesWas depends on how many indexes existed before removing the index.

Verify the remaining indexes:

db.students.getIndexes()

Key Takeaways

  • A multikey index is an index that MongoDB uses for fields containing arrays.
  • You create it with the normal createIndex() method.
  • MongoDB automatically makes the index multikey when the indexed field is an array.
  • Multikey indexes are useful for fields such as skills, tags, and arrays of embedded documents.
  • A query such as { skills: "Python" } can search for an element inside an array.
  • $in can match an array field against multiple possible values.
  • $all requires all specified values to exist in the array.
  • $elemMatch is useful when multiple conditions must apply to the same embedded array element.
  • A compound index can become multikey when one of its indexed fields contains an array.
  • explain("executionStats") can help inspect whether an index is being used.
  • getIndexes() displays indexes on a collection.
  • dropIndex() removes an index.
  • Multikey indexes have specific restrictions; for example, a compound multikey index cannot generally include more than one indexed field that is an array in the same document.
  • Indexes should be created based on actual query patterns because unnecessary indexes consume storage and add maintenance work.

FAQs

1. What is a multikey index in MongoDB?

A multikey index is an index used for a field that contains an array. MongoDB automatically makes the index multikey when the indexed field contains an array value.

2. How do you create a multikey index?

You create it using the normal createIndex() method.

db.students.createIndex({
  skills: 1
})

If skills is an array, MongoDB automatically treats the index as multikey.

3. Can a multikey index be created on an array of objects?

Yes. You can create an index on a field inside an array of embedded documents.

db.students.createIndex({
  "results.subject": 1
})

4. What is the difference between a normal index and a multikey index?

A normal index indexes scalar field values, while a multikey index handles fields whose values are arrays. MongoDB automatically determines that an index is multikey when appropriate.

5. Can $elemMatch be used with multikey indexes?

Yes. $elemMatch can be used to query arrays of embedded documents when multiple conditions need to match the same array element.

6. Can a compound index become a multikey index?

Yes. If a field included in a compound index contains an array, MongoDB can make that compound index multikey, subject to multikey index restrictions.

7. How do you check whether a multikey index is being used?

You can use explain("executionStats") with the query.

db.students.find({
  skills: "Python"
}).explain("executionStats")

An IXSCAN stage can indicate that an index scan is being used.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top