WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Intermediate · question 27 of 100

Explain the concept of index intersection in MongoDB.?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

Index intersection is a technique in MongoDB that allows to use more than one index to respond to a query, thus making the query more performant.

Normally, queries with multiple filter conditions may have to scan every document in a collection to find the appropriate results, which can be very slow for large collections. However, if we have multiple indexes that each cover parts of the query, we can perform an index intersection to achieve the same result.

For example, imagine we have a collection of books, and we want to find all the books that have been published after 2010 and that have a specific author. We could have separate indexes on the ‘published_date‘ field and the ‘author‘ field. MongoDB can then use both indexes to efficiently retrieve all documents that satisfy the query by using index intersection.

The following is an example of how index intersection works in a MongoDB shell. Suppose we have a collection of cars that contains the ‘manufacturer‘, ‘model‘, and ‘year‘ fields.

> db.cars.createIndex({manufacturer: 1})
> db.cars.createIndex({model: 1})
> db.cars.createIndex({year: 1})

Now suppose we want to find all cars manufactured by Toyota, model "Corolla", and produced after 2015. We can express this query as follows:

> db.cars.find({
    manufacturer: "Toyota",
    model: "Corolla",
    year: {$gt: 2015}
  })

We can see that we could create a compound index on ‘manufacturer: 1, model: 1, year: 1‘ that would cover this query. However, suppose we only have the three individual indexes we created earlier. MongoDB can use index intersection to combine these indexes to efficiently execute the query.

> db.cars.find({
    manufacturer: "Toyota",
    model: "Corolla",
    year: {$gt: 2015}
  })
  .hint("manufacturer_1_model_1_year_1")

In this example, we explicitly specify to the MongoDB query optimizer to use the compound index, even though we have separate indexes. MongoDB then intersects the indexes to find the documents that satisfy the query.

It is important to note that while index intersection can be useful, it is not always the best option. Creating a compound index that covers the query fields can often be more efficient, especially if the query is very selective. Additionally, index intersection can add overhead to the query if the indexes are not in memory. Therefore, as with any performance optimization in MongoDB, it is important to test and evaluate the most efficient approach for each use case.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics