Video Transcript (English)
Imagine you have a database with millions of documents where you only ever query a subset of them based on specific criteria. You could index the whole thing to support your query but is there a better way?
In this video we are going to take a look at partial indexes in MongoDB.
We will talk about what they are, some use cases and how to create one.
Let’s get started!
Partial indexes only index documents from a collection that match a filter expression. The result is reduced storage requirements and memory footprint for your index.
Although partial indexes can be created on nearly any field combination there are some restrictions to keep in mind.
`_id` cannot be added to a partial index because a full index of this field is required for the proper operation of the system.
Shard key fields cannot be added to a partial index for the same reason.
To create a partial index, we use the familiar db.collection.createIndex() method. The syntax is nearly identical to creating a single-field or compound index, with the addition of the `partialFilterExpression`option.
This option accepts a document that specifies the filter condition using any of the following expressions: Equality expressions using the $eq operator, Boolean expressions via $exists: true, Range expressions such as $gt, $gte, $lt, and $lte, Type expressions using the $type operator, and logical expressions using the $and, $or, and $in operators.
Say for example, we had a collection with population data based on a specific location. For example, here is a document from that collection that contains information like the city name, zip, location, population size, and state.
We’ll create a partial index on the state field using the partialFilterExpression that only indexes a subset of documents. In this example, areas with a population greater than or equal to 10,000 will be indexed.
Now let’s test our partial index by running a couple of queries with the explain method.
First, let’s search for the state of California to see if our index is being used. We're using the queryPlanner.winningPlan to only display relevant details from the explain output
Interesting, it seems like our index wasn’t used since our query had to do a collection scan.
This happens because we indexed documents with a population greater than or equal to 10,000 but there are also documents from california with a population less than ten thousand. Therefore, MongoDB didn’t use our partial index.
Let’s try filtering out documents in the state of California with a population below ten thousand and just query for documents that have a population greater than or equal to 10,000
Look at that! Our partial index was used which makes sense since we filtered out all of the documents with a population below ten thousand.
Let’s test one more query this time filtering out all documents with a population below twelve thousand.
What do you think will happen when we run it? Will our index be used or not?
If you guessed that it would not use our index then you are correct.
Again, this is because our query is also returning documents with a population below the threshold we set in the index.
How would you fix it? Visit our community forums to let us know!
Great job! In this video, you learned that partial indexes only index documents from a collection that match a filter expression.
We also learned how to create a partial index and discussed some of their limitations.