Lesson 4: Wildcard Indexes / Learn

Video Transcript (English)

One of the index types that you have at your disposal when using MongoDB are wildcard indexes.

Wildcard indexes support queries against unknown or arbitrary fields.

This index type is great for datasets with dynamic schemas.

For example, applications that deal with IoT, lots of metadata, or Weather services where it’s not always feasible to know if each document will have the same fields.

In this video, we’ll learn what wildcard indexes are, use cases, how to create one, and some of their limitations.

Let’s look at an example. Say we have a product catalog for a store that carries all types of items like food, clothes, home goods, etc.

Here we have two documents from the products collection. First, we have stereo speakers which have a product attributes field referring to the color, size, and weight of the speakers.

The next document in this collection is referring to information on our bread product. Bread has a completely different set of attributes in the product attributes field.

We will need to query these product attribute fields quite often so we should create an index for this. We could create multiple single field indexes for every unique product attribute field but this would end up taking up significant resources and reduce our schema flexibility.

Another option would be to change our data model such that product attributes holds an array of sub documents each with a field for a key and value.

Doing this allows us to create a compound index on the fields labeled k and v.

The problem with this approach is that it makes querying for these fields feel unnatural. We would have to use an elemMatch on the k and v fields.

Another approach is to keep our document the way it was and add a wildcard index to the product attributes field.

To create a wildcard index on the product attributes field, use the db.collection.createIndex() method, passing in the name of the field we want to index appended with a dot, dollar sign and two asterisks.

When created, this index will support arbitrary single field queries on product attributes or its embedded fields, such as: product_attributes.size, product_attributes.type, or product_attributes.crust

Let’s create this index in the MongoDB shell.

Now that the index is created, let’s test a query using the explain method and inspect the winning plan to confirm that our wildcard index is being used.

After running the command we can see that our wildcard index is being used by inspecting the index name which matches the name of our wildcard index.

We can also use wildcard indexes to create an index on all fields in each document of a collection.This is useful if you have very dynamic documents.

Similar to earlier, we use the dollar sign followed by two asterisks without any field path prefix.

This creates an index on all of our fields.

In most cases, documents aren’t just made up of fields with primitive values. We often have embedded documents or arrays. Wildcard indexes deal with each one of these differently.

If the field is an embedded document, the wildcard index descends into the object and indexes its contents. It will continue traversing any additional embedded documents until it reaches a primitive value.

If the field is an array, then the wildcard index traverses the array and indexes each element. Embedded documents in the array are indexed entirely as previously described.

By default, wildcard indexes do not index the _id field. If we want to also index the _id field we must use the wildcardProjection option. This allows us to specify which fields we want our wildcard index to include or exclude by specifying a one or zero. In this scenario, I am choosing to include the _id, while excluding the stock, and price fields.

Let’s try this out. In the shell, I will create our new index.

Next, let’s run a couple of queries to test our index. First, we’ll query using the sku number.

Good news, it appears that our newly created index has been used.

Next, let’s query for product attributes.

Look at that, our wildcard index is still in use.

Don’t forget that if there are multiple eligible indexes that produce the same exact score – mongodb will choose one arbitrarily.

As you can see, wildcard projection gives you the ability to better tailor your wildcard indexes.

Starting in MongoDB 7.0 you can create compound wildcard indexes. A compound wildcard index has one wildcard field and one or more additional index fields.

For example, we can create an index on the stock field and all of the product attributes subfields.

Some things to consider when creating wildcard indexes are:

You can have multiple wildcard indexes per collection.

Wildcard indexes may cover the same fields as other indexes in the collection.

You cannot use the unique or TTL options.

Awesome job! In this video, we learned that Wildcard indexes are special indexes that support queries against unknown or arbitrary fields.

Next, we learned how to create a wildcard index. We also used the wildcardProjection option to include or exclude certain fields from our wildcard index.

Finally, we covered some things to consider when using wildcard indexes.